Posts Tagged Array

How to make wordpress plugin: General

This article will present you with the basics of wordpress plugin. I have create a plugin to help you understand how to create a plugin. This simple plugin shows a digg button to all your post. You can download the full code here. Below is several code I use:

class diggme
{
function diggme() {

add_action(’init’, array($this, ‘init’));
add_action(’admin_menu’, array(&$this, ’setmenu’));
add_filter(’the_content’, array(&$this, ‘the_content_filter’));
}

function init()
{
?>
<link href=”<?php echo get_option(’siteurl’); ?>/wp-content/plugins/diggme/css.css” rel=”stylesheet” type=”text/css” />
<?php
}

function managepage(){
echo “<div id=’setting’>This is setting page. Just ignore this</div>”;
}

function setmenu () {
add_options_page(
‘DiggMe’,
‘DiggMe’,
‘manage_options’,
__FILE__,
array(&$this,’managepage’ ) );
}

function the_content_filter($content)
{
return $this->displaydigg() . $content;
}

function displaydigg()
{
global $post;

return “<div id=\”diggbutton\”><a href=\”http://digg.com/submit?phase=2&amp;url=”.get_permalink($post->ID).”\”><img src=\”".get_settings(’siteurl’).”/wp-content/plugins/diggme/digg.png\”></a></div>”;
}

}

$diggme = new diggme();

  • The code will first create a class called diggme. Every time the plugin loads it will create an object of this class using $diggme = new diggme();

The constructor

  • add_action(’init’, array($this, ‘init’)); -> This will tell your blog to execute function init whenever it loads.
  • add_action(’admin_menu’, array(&$this, ’setmenu’)); -> This will tell your blog to create menu at your admin page. You can place any settings here.
  • add_filter(’the_content’, array(&$this, ‘the_content_filter’)); -> This will be executed every time when your post loads. In this example we will load function the_content_filter every time the page loads, read the post content and add a digg button to the post.

Other function is easy to understand if you have basic PHP skill. If you have any question just comment below.

, , , , , , , , , , , , , , , , , , , ,

1 Comment

SOAP PHP example

This article will show how to do web service using SOAP (Simple Object Access Protocol). You can use SOAP using your PHP. It is usually enabled by default. You can check whether you have SOAP in your phpinfo setting.

For the example we will be using this WSDL. You can see that it have the TopGoalScorers operation:

<operation name=”TopGoalScorers”>
<documentation>
Returns an array with the top N goal scorers and their current score. Pass 0 as TopN and you get them all.
</documentation>
<input message=”tns:TopGoalScorersSoapRequest”/>
<output message=”tns:TopGoalScorersSoapResponse”/>
</operation>

And the operation have the input message:

<message name=”TopGoalScorersSoapRequest”>
<part name=”parameters” element=”tns:TopGoalScorers”/>
</message>

The input operation have one parameter from the element “TopGoalScorers“. The parameter is iTopN which have int value.

<xs:element name=”TopGoalScorers”>
<xs:complexType>
<xs:sequence>
<xs:element name=”iTopN” type=”xs:int”/>
</xs:sequence>
</xs:complexType>
</xs:element>

The return value from the output message is ArrayOftTopGoalScorer:

<xs:element name=”TopGoalScorersResponse”>
<xs:complexType>
<xs:sequence>
<xs:element name=”TopGoalScorersResult” type=”tns:ArrayOftTopGoalScorer”/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name=”ArrayOftTopGoalScorer”>
<xs:sequence>
<xs:element name=”tTopGoalScorer” type=”tns:tTopGoalScorer” minOccurs=”0″ maxOccurs=”unbounded” nillable=”true”/>
</xs:sequence>
</xs:complexType>

The return element is tTopGoalScorer, have two value sName (string) and iGoals (int).

<xs:complexType name=”tTopGoalScorer”>
<xs:sequence>
<xs:element name=”sName” type=”xs:string”/>
<xs:element name=”iGoals” type=”xs:int”/>
</xs:sequence>
</xs:complexType>

So the code will be:

$client = new SoapClient(”http://euro2008.dataaccess.eu/footballpoolwebservice.wso?WSDL”);

$params = array(’iTopN’ => 3);

result = $client->TopGoalScorers($params);

print_r($result->TopGoalScorersResult);

, , , , , , , , , , , , , , , , , , , , , , ,

No Comments