How to create a Wordpress shortcode-style function in PHP

戏子无情 提交于 2019-12-30 06:36:22

问题


I am trying to create a Wordpress shortcode-style feature in PHP to replace shortcodes like "[[133]]" with images. Basically, I have a MySQL table of image URLs/titles/subtitles with IDs 1-150, and I want to be able to dynamically insert them into the text of my pages with shortcodes like this:

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 feed it to a MySQL query like mysql_query("SELECT title,subtitle,url FROM images WHERE id = $id") and then replace the "[[id]]" with the img/title/subtitle. I would like to be able to do this multiple times on the same page.

I know this has to involve regex and some combination of preg_match, preg_replace, strstr, strpos, substr... but I don't know where to start and which functions I should be using to do which things. Can you recommend a strategy? I don't need the code itself—just knowing what to use for which parts would be extremely helpful.


回答1:


If you want to be able to write shortcodes like this :

[[function_name_suffix parameter1 parameter2 ...]]

here is a more complete way, using preg_replace_callback and call_user_func_array to implement parameterized shortcodes.

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]]");

Will output :

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

This is just something I implemented right now based on supertrue's idea.

Any feedback is more than welcome.




回答2:


With a function getimage($id) that does the MySQL query and formats the replacement text, this almost does everything 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 ????? is) that will make it put in the right image for the right [[id]].

Refer preg_match_all and preg_replace on official documentation for more details.




回答3:


Various different approaches can be taken for this, depending on how you plan to display ect,

Take the sentence "Hello [34] world"

Create a simple function e.g 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 anymore occurrences of brackets are found, recursively call the function again, passing the rest of the string to the function again.

Note: Validation may need to be done first to ensure the [ and ] are properly balanced!




回答4:


My bet is PHP's strtr function...

<?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);

it's output is

[http://php.net/images/logos/php-logo.svg] My name is Vishal Kumar Sahu. I am a Web Designer and I love to Draw and Paint. I don't know Any Programming Language but I know PHP, JAVASCRIPT AND HTML little bit. Here is my profile image [Vishal-Kumar-Sahu-Web Designer]

It is working fine on 5.6.




回答5:


The regex, I believe, would be:

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

(for a 1-to-3 digit number inside double brackets.)



来源:https://stackoverflow.com/questions/4568463/how-to-create-a-wordpress-shortcode-style-function-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!