Saturday, July 27, 2013

Options Panel - day 9

So continuing from yesterday I'm working on the number of posts per page option.  The first thing I was working on was choosing whether to remove numbers using jquery on keyup or using php and just changing the input type.  The jQuery version looks like this:
//only allow numbers to be entered
$('#number_of_posts').keyup(function(e){
if(/\D/g.test(this.value)){
this.value = this.value.replace(/\D/g, '');
}
});

I got this solution from here: http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery

This is the php function which involved me changing the textarea callback:
function b4b_textarea_callback($args){
$options = get_option('b4b_theme_display_options');
if($args[0]=="number_of_posts"){
$html = '<input type="number"';
$html .= ' pattern="\d*"';
} else {
$html = '<input type="text"';
}
$html .= ' id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" value="' . $options[$args[0]] . '" />';

echo $html;
}

I got help to find this solution from here: http://stackoverflow.com/questions/8808590/html5-number-input-type-that-takes-only-integers
Rather than replacing anything besides numbers with letters right away this allows for the letters to be entered and doesn't allow them to be saved.  I've decided to use the php solution instead of the jQuery one because letters can still be saved if the user clicks the save button right away.

Lastly this is the php code I made for querying the amount of posts:
<?php
$options = get_option( 'b4b_theme_display_options' );
$number_of_posts = $options['number_of_posts'];

if($number_of_posts != ''){
$args = 'posts_per_page=' . $number_of_posts;
query_posts($args);
}
?>


Next is the enabling sharing in posts option.  Planning this more in detail, this option will have a sharing button for Facebook, Twitter, Email and Google Plus.  These developer pages will help me create the code I need for the share buttons:

As for the email share button, that's something I want to try writing from scratch as a learning experience.  As of now I have a headache so I'm leaving this option for tomorrow.

No comments:

Post a Comment