How to create a wordpress shortcode style function in PHP

I am trying to create a PHP function in the Shortcode style in PHP to replace shortcodes like "[[133]]" with images. Basically, I have a table of MySQL image / title / subtitle URLs with ids 1-150, and I want to be able to dynamically insert them into the text of my pages using short codes, such as:

Blabla bla bla bla bla. [[5]] Also, bla bla bla bla bla [[27]] Hey, and bla bla bla! [[129]]

So, I just want to grab the ID as $ id and then pass it to a MySQL query, for example mysql_query ("SELECT header, subtitles, url FROM images WHERE id = $ id") and then replace "[[id]]" with img / title / subtitle. I would like to be able to do this several times on the same page.

I know this should include a regex and some combination of preg_match, preg_replace, strstr, strpos, substr ... but I don’t know where to start and what functions I should use to do something. Can you recommend a strategy? I don’t need the code itself - just knowing what to use, for which the parts will be extremely useful.

+6
php wordpress shortcode
source share
5 answers

Using the getimage($id) function, which executes a MySQL query and formats the replacement text, is almost all you need:

 $text = "Blabla [[5]] and [[111]] bla bla bla [[27]] and bla bla bla! [[129]]"; $zpreg = preg_match_all('#\[\[(\d{1,3})\]\]#', $text, $matches ); var_dump( $matches[1] ); $newtext = preg_replace('#\[\[(\d{1,3})\]\]#', getimage($matches[1][?????]), $text); echo $newtext; 

I just need to figure out what to put inside getimage() (where ?????), which will force it to insert the correct image for the right [[id]] .

Refer to preg_match_all and preg_replace for official documentation.

+4
source share

If you want to write short codes, for example:

 [[function_name_suffix parameter1 parameter2 ...]] 

here's a more complete way, using preg_replace_callback and call_user_func_array to implement parameterized call_user_func_array .

 function shortcodify($string){ return preg_replace_callback('#\[\[(.*?)\]\]#', function ($matches) { $whitespace_explode = explode(" ", $matches[1]); $fnName = 'shortcode_'.array_shift($whitespace_explode); return function_exists($fnName) ? call_user_func_array($fnName,$whitespace_explode) : $matches[0]; }, $string); } 

If this function is defined:

 function shortcode_name($firstname="",$lastname=""){ return "<span class='firstname'>".$firstname."</span>&nbsp;<span class='lastname'>".$lastname."</span>"; } 

Then this call

 print shortcodify("My name is [[name armel larcier]]"); 

It will display:

 My name is <span class='firstname'>armel</span>&nbsp;<span class='lastname'>larcier</span> 

This is just what I implemented right now based on the idea of ​​supertrue.

Any feedback is more than welcome.

+5
source share

You can use different approaches for this, depending on how you plan to display the ect,

Take the sentence "Hello [34] world"

Create a simple function like replaceCode ($ string)

 function replaceCode($string){ $pos = strpos($string, '['); // Find the first occurrence of the bracket if($pos != false){ // If everything is ok take the next 2 numbers from it // Check for a close bracket & remove ] // call another function to replace the number with the image text } } 

If more parentheses are found, call the function recursively again, passing the rest of the line again.

Note: you must first check to make sure that [and] are properly balanced!

+1
source share

The regular expression, I suppose, would be:

 /\[\[[1-9]{1,3}\]\]/g 

(for numbers from 1 to 3 digits inside double brackets.)

0
source share

My bid function is PHP strtr ...

 <?php function get_profile_image($image_url){ return "<img src='{$image_url}' height='200px' width='200px' />"; } $trans = array( "[[1]]" => "Vishal", "[[2]]" => "Kumar", "[[3]]" => "Sahu", "[[4]]" => "Web Designer", "[[5]]" => "Draw and Paint", "[[6]]" => ucwords("any programming language"), "[[7]]" => strtoupper("PHP, JAVASCRIPT and HTML"), "[[8]]" => get_profile_image("http://php.net/images/logos/php-logo.svg"), "[[9]]" => "http://php.net/images/logos/php-logo.svg" ); $str = <<<HEREDOC_1 [[8]] <pre>My name is [[1]] [[2]] [[3]]. I am a [[4]] and I love to [[5]]. I don't know [[6]] but I know [[7]] little bit.</pre> Here is my profile image <img src='[[9]]' alt='[[1]]-[[2]]-[[3]]-[[4]]' /> HEREDOC_1; echo strtr($str, $trans); 

is displayed

[ http://php.net/images/logos/php-logo.svg] My name is Vishal Kumar Sahu. I am a web designer and I like to draw and draw. I don’t know Any programming language, but I know PHP, JAVASCRIPT AND HTML a little bit. Here is my profile picture [Vishal-Kumar-Sahu-Web Designer]

It works great on 5.6.

0
source share

All Articles