Alcaidesa

Toodles England, Ola Spain!

Time for a new venture. I have moved from the UK and now reside in Spain. A big change when you can’t speak the local language, however also a great opportunity to learn a new non programming language!

I hope this new move will mean that I’m out and about more than thus will be able to blog about fun activities and nice images as well as my normal programming rants!

SpotifyLibImg

My Spotify Meta Api Library for PHP

About 6 weeks ago I completed the first version of my library to work with Spotify’s Meta Api. However I realised that I had forgotten to mention it on my blog. You can view to source on github ( https://github.com/irvingswiftj/spotifyLib )

Features:

  • Search for songs/artists/albums/tracks on Spotify
  • get info on a song/artist/album/track by uri
  • Unit tests built with PHPUnit
  • Using composer for phpunit and guzzle dependency

Still to do:

  • Implement PSR-0 autoloading
  • Go through my code with PHP-cs!

Any feedback is always welcome.  I posted this on reddit, and I was really impressed with feedback from the community ( http://www.reddit.com/r/PHP/comments/20ju0v/created_a_php_library_for_using_spotifys_api/ ).

Setting up Xdebug’s remote debug with PHPStorm

Introduction

As promised in a reddit discussion, I thought I would put up an article on setting up xdebug’s remote debugger with PHPStorm. I have had to assist multiple developers in setting it up and I suspectits because there is a lack of ‘how tos’ on this topic on the interwebs.

I have broken this ‘how to’ into two sections, the server setup and the PHPStorm setup. In my case my server happens to be Debian, so if you are using a different flavour of linux, please adjust as necessary. Likewise with PHPStorm version, my screenshots are from version 7, as far as I can remember, they should differ since version 5.

The server settings

Firstly, check that you have Xdebug installed. My method of checking this is to run the following from terminal:

$ php -i | grep "xdebug"

If you get a result, fanstastic, else you will need to install xdebug, this should do it for you:

$ apt-get install php5-xdebug

Now you will need to change you php settings, so open the file /etc/php/conf.d/xdebug.ini (hopefully using vim and we all know that nano is an inferior editor!!!).

It should contain one line which will be the setting for the zend_extension. After that line add the following settings:

debug.remote_host= 192.168.0.10
xdebug.remote_port=9000
xdebug.max_nesting_level=200
xdebug.remote_enable=1
xdebug.idekey="PHPSTORM"
xdebug.remote_autostart = 1

N.B. if you are unsure of what your remote host IP address is, create a php file in which you echo $_SERVER['REOMTE_ADDR']; and run it from the machine you use PHPStorm with. It will output the correct IP address.

Finally, don’t forget to restart you web server (apache in this example):

$ service apache2 restart

The settings in PHPStorm

Go to your PHPStorm settings and make sure that you xdebug port is set to 9000 (if you can not use 9000, I sometimes 9124 else 9125; just make sure it is the same in you ini settings on your server).

PHPStorm Settings Panel
PHPStorm Settings Panel

Save those settings and now go to Run>Edit Configurations… where you will need to add a remote debug instance (see screenshot below)

add remote debug instance
add remote debug instance

You can name this remote debug instance what ever you want, just make sure the IDE key matches what is in xdebug.ini on your server (in this example we used ‘PHPSTORM’).

remote debug instance settings
remote debug instance settings

It likely you won’t have a server setup, if that is the case you can add one by clicking the ‘…’ button. In my project when adding a server, I always need to specify the mappings so you might find you have to do the same.

Finally, once have saved those settings, click on the listen button so that it goes from:

xdebug listening icon
xdebug listening icon

to:

xdebug not listening icon
xdebug not listening icon

If you add a breakpoint now, and load up the page that you want to debug, it should now stop at your breakpoint (I would probably test this with the first line of your index.php to make sure your breakpoint is getting hit!).

Hopefully you will now be a god at debugging the hardest of bugs!

Other hints

If you are not using xdebug, i highly recommend that you turn it off on your server, as if you computer is turned off and you access anything PHP via your webserver it will hang like make as it will be looking for the machine with the IP address specified.

I recommend that you check out xdebug’s profiler if you haven’t already and use it with a tool like kcachegrind else webgrind. Again, I would turn off profiling when not using it as the files it creates on each page load are vast and if you have a small server you may find you run out of storage very quickly!

My Lack of Blogging + Apology!

So, I have written anything to my blog for about 18months, one of my new year’s resolution is to blog more. I have notice that this site is painfully slow, hence new hosting is high on my list of priorities.

I was also tired of the old theme on this blog, so I have set it to WordPress’ new twenty fourteen theme until I rustle up the time to create something custom.

At least my resolution for this year was greater that 1024×768!

Quick Start guide to install LEMP server on Ubuntu 12.04 (Linux + Nginx + MySql + PHP )

I have seen some online guides about this but really was after one specific to Ubuntu 12.04. So here goes:

Step 1 – Installing Nginx

First we need to add the Nginx source

sudo add-apt-repository ppa:nginx/stable

Next, as we always do after adding a source, we need to update

sudo apt-get update

And now we install it

sudo apt-get install nginx

Next, we must start the Nginx service.

sudo /etc/init.d/nginx start

To check if it is successful, try going to http://localhost/ in your browser. If you see a Welcome to Nginx page, it has been successful.

Step 2 – Installing PHP5:

Install PHP and any required extensions

sudo apt-get install php5-cli php5-cgi php5-fpm php5-mcrypt php5-mysql

Edit the default Nginx site config (if you are not familiar with vim, try entering ‘nano’ instead)

sudo vim /etc/nginx/sites-available/default

The changes you will need to make are as follows:
1)

index index.html index.htm;

to

index index.html index.htm index.php;

2)

root /srv/http/nginx;

^this path may be different in your install, it doesn’t matter as we are changing it!
to:

root /var/www;

3) uncomment the following lines

 location ~ \.php$ {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index index.php;
     include fastcgi_params;
  }

Now quit vim (‘:wq’) and restart the nginx service.

sudo /etc/init.d/nginx restart

If the folder /var/www/ doesn’t exist, create it

sudo mkdir /var/www/

Step 3 – Install MySQL

And finally,

sudo apt-get install mysql-server

Step 4 – Check it works!

Create a test file

vim /var/www/test.php

Add this line

<?php phpinfo();?>

Save and quit vim (:wq) and go to http://localhost/test.php in your browser.
If you see a page displaying all your PHP settings, then it works!

Ajax breaks in wordpress if back-end uses SSL

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!

Guide to WordPress Settings API

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/

James Irving-Swift's portfolio and random blogging