Default Green Orange
Swifty's Blog
James Irving-Swift's portfolio and random blogging
RSS
  • Home Page Home
  • My CV

Wordpress Category

Ajax breaks in wordpress if back-end uses SSL

Development 2 Comments »

So you have a wordpress blog and you (or your client) want the wp-admin to use an SSL certificate, sounds great! However, any AJAX you use in your site has broken. I spent a while googling this issue when it happened to me and found a really easy fix; you need to change the variables in the get_admin_url() function to force the AJAX requestion to use http instead of https. Change it to look like:

get_admin_url(null,'','http');

I hope this helps a lot of people!


March 3rd, 2012  



Guide to WordPress Settings API

Development, Wordpress 0 Comment »

I have decided to explain how to use the WordPress Settings API after having pulled out most my hair whilst googling for a easy to understand explanation.

The reason I wanted to use the WordPress Settings API was because whilst making plugins, I found that my code for my options page was getting a little messy. I had also read, that if you use the Settings API, you don’t ever have to use the update_option() function.

Finally, being a WordPress API, I would like to think that this make my plugins a little more future-proof against wordpress upgrades.

Please note that in this walkthrough I am storing the options into an array.

So this is how it is done:

add_action('admin_menu', 'create_plugin_options_page');
add_action('admin_init', 'register_and_build_plugin_options');

function create_plugin_options_page() {
  //register the menu item
  add_menu_page('Plugins Option', 'Plugin Options', 'administrator', 'option-page-name', 'plugin_options_page');
}

function register_and_build_plugin_options(){
  //register the option value you will be using.
  //N.B. the second parameter must be the same as what you have named the option in your plugin activation
  register_setting('settings_option','plugin_settings', 'validate_option');
  add_settings_section('first_setting_section', 'Settings Section 1','section1_callback','option-page1');
  add_settings_field('setting_text','Text Field: ','text_field','option-page1','first_setting_section');
}

You can have lots of fields in lots of sections. Important things to note when using add_settings_section, add_settings_field, register_setting:

  • The 3rd parameter of register_setting is a callback for validation. In the example above, I have a function (shown later) called validate_option which contains any required validation. This field is optional.
  • The 4th parameter of add_settings_field must correlate to a 1st parameter of add_settings_section
  • The 4th parameter of add_settings_section which should be the same as the 4th parameter of add_settings_field (‘option-page1′) is what you will later use to load this section by using the function do_settings_section();
  • The 3rd parameter of add_settings_section is a callback function. It can be a blank function if you want it to be.
  • The 3rd parameter of add_settings_field is a callback function that contains the input field that you want.
This is the callback function with input field:
function text_field(){
	  $optionValue = get_option('plugin_settings');
    $option = "";
    $option .= "";
    echo $option;
}

You must name the field by the option name so it will save correctly, like I have done above with name=\”plugin_settings[text_field]\”.

Now finally, the function that brings it all together:

function plugin_options_page() {
?>
<div id="theme-options-wrap">
<h2>Plugin Title</h2>
<div><form action="options.php" method="post"> 
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php esc_attr_e('Save Changes'); ?>" /></p>

</form></div>
</div>
<div id="theme-options-wrap">
<h2>Plugin Title</h2>
<div><form action="options.php" method="post"> 
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php esc_attr_e('Save Changes'); ?>" /></p>

</form></div>
</div>
<?php
}

function validate_option($option){
    //put any validation on the options here.
    return $option;
}

function section1_callback(){}

function section2_callback(){
	$html = "An example of some text in the callback function";
	echo $html;
}

function text_field(){
	  $optionValue = get_option('plugin_settings');
    $option = "";
    $option .= "
<input id=\"text_field\" type=\"text\" name=\"plugin_settings[text_field]\" value=\"{$optionValue['text_field']}\"?-->";
    echo $option;
}

function select_field(){
    $options = get_option('plugin_settings');
    $items = array("Red", "Green", "Blue", "Orange", "White", "Violet", "Yellow");
    $html = "
    <select id="select_field" name="plugin_settings[select_field]">";
    foreach($items as $item) {
        $selected = ($options['select_field']==$item) ? 'selected="selected"' : ''; $html .= " <option selected="selected" value="$item">$item</option> ";
    }
    $html .= "</select>";
    echo $html;
}
function checkbox_field(){
    $optionValue = get_option('plugin_settings');
    $option = "";
    $option .= " <input type="\"checkbox\"" name="\"plugin_settings[checkbox_field]\"" value="\&quot;1\&quot;" checked="checked" />";
    echo $option;
}
function radio_field(){
    $optionValue = get_option('plugin_settings');
    $option = "";
    $option .= " <input type="\"radio\"" name="\"plugin_settings[radio_field]\"" value="\"value1\"" checked="checked" />Value1 ";
    $option .= " <input type="\"radio\"" name="\"plugin_settings[radio_field]\"" value="\"value2\"" checked="checked" />Value2 ";
    echo $option;
}

This will allow you to have a settings page that looks like:

Quite a Mouthful! However I hope this really helps for people trying to get to grips with API. Admittedly, I have written this partly as a response to lack of documentation on the API!

If you would like more help, I found this documentation helpful: http://www.presscoders.com/wordpress-settings-api-explained/


November 29th, 2011  



Filtering custom posts by more than one Taxonomy

Development, Wordpress 0 Comment »

With WordPress now able to use custom post types, I have asked on forums if anyone new how to filter by more than one taxonomy. I originally tried using

 query_posts('cat=6,5') 

and other similar commands but have no luck.

To resolve this I came up with the following solution:

First I create two custom fields on the page which I named taxonomy1 and taxonomy2.
I then added the following code:

 <?php 
$taxonomy1 = get_post_meta($post->ID, "taxonomy1"); 
$taxonomy2 = get_post_meta($post->ID, "taxonomy2");

//this is an sql query to find all posts containing two taxonomies
$sql_query = "SELECT * FROM (
			SELECT wp_posts.ID FROM `wp_posts` 
				JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
				JOIN wp_terms ON wp_term_relationships.term_taxonomy_id=wp_terms.term_id
					WHERE slug = '".$taxonomy1[0]."' 
			UNION ALL
			SELECT wp_posts.ID FROM `wp_posts` 
				JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
				JOIN wp_terms ON wp_term_relationships.term_taxonomy_id=wp_terms.term_id
					WHERE post_type = 'tiles' AND slug = '".$taxonomy2[0]."'
		) AS results

		GROUP BY results.id HAVING COUNT(*) = 2";
		
	$newresults = $wpdb->get_results($sql_query);			
		
	$ArrayOfIDs = array();
		
	foreach($newresults as $newresult){
		array_push($ArrayOfIDs, $newresult->ID);
	}

	query_posts(array('posts_per_page'=>'900','post_type' => 'tiles','post__in' => $ArrayOfIDs));
?>

The idea behind this code is that the mysql query returns the post IDs of the posts that correspond to the two taxonomy filters and then the foreach loop pushes those into an array of IDs ($ArrayOfIDs) which is then used in the query_post function.

Feedback would be much appreciated on other ways that I could have resolved this issue or also how I could improve this code.


October 7th, 2010  



  • Search

  • Calendar

    May 2013
    M T W T F S S
    « May    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • My Twitter

    • @skyatlantic why is sky atlantic HD the only channel that apparently can get signal?!? 1 day ago
    • @miniviolette it was… I realised that I was to drunk to continue when I only got one kill in battlefield3 in 30 mins!!!! epic fail! 4 days ago
    • @miniviolette yeah! i just stay in an played xbox and didn't pay attention to how much I was drinking at the same time! Amateur Mistake! 4 days ago
    • This sums up my Saturday morning! http://t.co/yBuMQ0afMy 4 days ago
    • @Rich_McCartney I'm still on Lion, i really need to sort out mountain lion so that my thunderbolt display stops being buggy! 4 days ago
  • Categories

    • Development
    • Development
    • JISCMail
    • Just for fun
    • Linux
    • Magento
    • Mailtalk
    • Mutiny
    • Random
    • Thomas Murr Art
    • Ubuntu
    • Websites
    • Wordpress
  • Archives

    • May 2012
    • March 2012
    • November 2011
    • May 2011
    • October 2010
    • September 2010
    • July 2010
    • April 2010
    • March 2010
    • SN205662 Samsung digital camera
    • SN205661 Samsung digital camera
    • SN205660 Samsung digital camera
    • SN205658 Samsung digital camera
    • SN205659 Samsung digital camera
    • SN205657 Samsung digital camera
    • SN205656 Samsung digital camera
    • SN205655 Samsung digital camera
    • SN205654 Samsung digital camera
    • SN205653 Samsung digital camera
    • SN205651 Samsung digital camera
    • SN205652 Samsung digital camera
    • SN205650 Samsung digital camera
    • SN205649 Samsung digital camera
    • SN205648 Samsung digital camera
    • SN205644 Samsung digital camera
    • SN205625 Samsung digital camera
    • SN205639 Samsung digital camera
    • SN205626 Samsung digital camera
    • SN205636 Samsung digital camera
Copyright © 2013 Swifty's Blog
XHTML CSS Log in