How to check if a post has gallery WordPress

Posted August 3rd, 2010 in PHP, Wordpress (wp) by Vlad Vornicu

I needed for a wordpress project to found out if a post has gallery inserted. I did a quick research and didn’t found anything that could help me (or maybe I searched wrong)… I thought wordpress has a function that should check if a post has gallery or not, like “has_post_thumbnail()”. Ok… so here is the simple solution I could make

$check_gallery = stristr(get_the_content(),'[gallery');
 
if($check_gallery == FALSE) {
    echo "this post doesn't have a gallery";
} else {
    echo "this post has gallery";
}

Contact Form 7 validation with default values

Posted May 6th, 2010 in Internet, PHP, Scripts, Wordpress (wp) by Vlad Vornicu

Just another contact form plugin. Simple but flexible.

Contact Form 7 can manage multiple contact forms, plus you can customize the form and the mail contents flexibly with simple markup. The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on.

You can get it from http://contactform7.com/

Ok… that’s enough with the introduction. I saw that many guys that use this wp plugin have a problem with the validation when they add default values to their inputs. So… I wrote down a simple solution for their problem.

Go to the plugin folder -> modules -> text.php and search for the function wpcf7_text_validation_filter (that is around line 107). Now, insert into the function the following lines

$values = (array) $tag['values'];
 
// Value
if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted() ) {
       if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['ok'] )
              $value = '';
       else
              $value = stripslashes_deep( $_POST[$name] );
} else {
       $value = isset( $values[0] ) ? $values[0] : '';
}
 
if($_POST[$name] == $values[0]) $_POST[$name]= '';

And that should solve your contact form validation problem. Let me know if I did something wrong.

How to check if the scrollbar is visible

Posted April 24th, 2010 in Browsers, Scripts, jQuery by Vlad Vornicu

Don’t know if somebody needs this, but I had to use it. Let’s say you want to find out if your page has a scroll bar. We will use a simple jQuery function that will return true if the scroll bar is visible and false if not.

function checkScrollBar() {
    var hContent = $("body").height(); // get the height of your content
    var hWindow = $(window).height(); // get the height of the visitor's browser window
 
    if(hContent>hWindow) { // if the height of your content is bigger than the height of the browser window, we have a scroll bar
        return true;    
    }
 
    return false;
 
}

Note. I didn’t have time to test this function. You can do it for me and notify if I did something wrong.

Radio button on Opera Mini

Posted April 10th, 2010 in Browsers, Html/Css, Internet by Vlad Vornicu

Recently, I had to make a form where you had to choose some answers. I said: “no problem”. (btw I had to make it work on opera mini). I made the form with the input radio in it

<form action="saveForm.php" method="POST" name="question">
      <label for="answer">Do you like my opera mini webpage?</label>
      <input type="radio" name="answer" value="1"> Yes <br/>
      <input type="radio" name="answer" value="0"> No
 
      <p><input type="submit" value="Submit" name="submit"></p>
</form>

and I tested. Surprize, the radio buttons didn’t work on opera mini.

I searched on Google for a solution…but didn’t find anything about radio button on opera mini (or maybe I didn’t search well). I made my own solution…it isn’t a great idea, but it works.

<p>Do you like my opera mini webpage?</p>
 
<form action="saveForm.php" method="POST" name="answer1">
      <input type="hidden" name="question" value="1">
      <a href="javascript: document.answer1.submit();">Yes</a>
</form>
 
<form action="saveForm.php" method="POST" name="answer2">
      <input type="hidden" name="question" value="0">
      <a href="javascript: document.answer2.submit();">No</a>
</form>

I hope it will be useful for someone.

How to improve your webpage with jQuery

Posted March 27th, 2010 in Internet, jQuery by Vlad Vornicu

I’ll share my few knowlege about jQuery. I’m not an expert on jQuery, I’m still learning how to use jQuery.

Ok let’s say you have a simple webpage, without animation, without nothing. Only the (x)html and the css stuff.

First you have to add in your html file (between <head> and </head>) the following line

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

Ok, this is how we get the latest version of the jQuery library.Now… let’s see. You want to show a message when somebody click on a button or a link. Let’s say you have a page that is under construction and you don’t want the visitor to get there before it’s ready.Also you don’t want him to reload the page for nothing to see. What you have to do: add an id for the link or button and a div that it will hidden until the visitor clicks the link.

<a href="#" id="underConstruction">Services</a>
 
<div class="showMessage">Sorry, but this page is under construction</div>

Now…let’s bring jQuery in our code. Add the following lines after the body:

<script type="text/javascript">
 
$(document).ready(function() {
 
       $(".showMessage").hide(); //here we will hide our message
 
       $("#underConstruction").click(function() {
 
              $(".showMessage").css("color","red"); //we made the message text red
 
              $(".showMessage").show(); //here we will show the message after the visitor clicks on the link
 
       });
 
});
 
</script>

I know, it’s looks like a piece of nothing for the guys who know jQuery but I know that on the internet are guys who are looking for this simple code. Ok, if you don’t understand my version, you can also check the documentation from jquery.com where you have more details. I hope it will help somebody.