1

我需要一些帮助,我希望你能帮助我。

我是一个初学者 WordPress 开发人员,我正在尝试开发一个简码插件,它允许我随时在 WP Admin 中添加新的简码并在前端显示它们。

我没有使用 WP 帖子类型。我创建了一个单独的表,其中只有 2 个字段:shortcode_title 和 shortcode_value。我可以添加、编辑和删除简码。

问题是我必须将每个“shortcode_title”注册为简码。我无法循环运行 add_shortcode 相关函数,我被卡住了。

将短代码添加到数据库中的代码:

if($_REQUEST['param']=="save_shortcode"){

    $form_data = array(
    'shortcode_title' => sanitize_text_field( $_REQUEST['frm_title'] ),
    'shortcode_value' => sanitize_text_field( $_REQUEST['frm_value'] )
     );

    // save data into db table
    $wpdb->insert("ds_shortcodes", $form_data);
}

现在我已经添加了 1 个简码,并且我已经编写了一个函数来将其添加为 WordPress 中的简码。

function ds_shortcodes_page_functions_DSexe(){
global $wpdb;
$shortcode_id = "Software";
$shortcode_details = $wpdb->get_row("SELECT shortcode_value FROM ds_shortcodes WHERE shortcode_title like '$shortcode_id'", ARRAY_A);

return $shortcode_details['shortcode_value'];
}

add_shortcode("Software","ds_shortcodes_page_functions_DSexe"); 

但是对于我在管理员中添加的每个短代码,我必须在 PHP 中手动复制这个函数。

我试过这个:

function shortcode_content(){

global $wpdb;
$all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes");

    foreach($all_shortcodes as $each_shortcode){
        return $each_shortcode['shortcode_value'];
    }

}

function shortcode_add(){

    foreach($all_shortcodes as $each_shortcode){
       add_shortcode($each_shortcode->shortcode_title,'shortcode_content');
    }

}

我必须循环从数据库中提取所有记录,并且对于每个“shortcode_value”,我必须将对应的“shortcode_title”添加为简码。

如果我 print_r $all_shortcodes 我得到一个这样的数组:

Array
(
[0] => stdClass Object
    (
        [shortcode_title] => Software
        [shortcode_value] => test.exe
    )

[1] => stdClass Object
    (
        [shortcode_title] => Version
        [shortcode_value] => 10
    )

[2] => stdClass Object
    (
        [shortcode_title] => Size
        [shortcode_value] => 412 MB
    )

[3] => stdClass Object
    (
        [shortcode_title] => DS1
        [shortcode_value] => 11111111111111
    )

[4] => stdClass Object
    (
        [shortcode_title] => DS2
        [shortcode_value] => 22222222222222
    )

[5] => stdClass Object
    (
        [shortcode_title] => DS3
        [shortcode_value] => 33333333333333
    )

)

我也尝试过的是:它似乎更有意义,但仍然缺少一些东西。

function shortcode_content($short_title, $short_value){
  global $wpdb;
  $all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes", ARRAY_A);
  foreach($all_shortcodes as $each_shortcode){
    $short_title =  $each_shortcode['shortcode_title'];
    $short_value =  $each_shortcode['shortcode_value'];
    return $short_title;
    return $short_value;
  }

}


function shortcode_add(){

  foreach($short_value as $key){
    add_shortcode($short_title,array('shortcode_content'));
  }

}

第二个函数似乎没有从第一个函数中获取变量。

我还将第一个函数中的变量放入数组中,但仍然不起作用。

function shortcode_content($short){
  global $wpdb;
  $all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes", ARRAY_A);
  foreach($all_shortcodes as $each_shortcode){
    $short_title =  $each_shortcode['shortcode_title'];
    $short_value =  $each_shortcode['shortcode_value'];
    $short =  array ($short_title, $short_value);
    return $short;
  }

}

function shortcode_add(){

  foreach($short_value as $key){
    add_shortcode($short_title,array('shortcode_content'));
  }

}

我想我越来越近了,但它仍然不起作用。:)

我知道这应该很容易,但作为初学者,它似乎相当复杂。

经过 2 天的尝试,我非常感谢您的帮助。

谢谢你。

阿林

4

2 回答 2

0

您是否注意到,当您执行 print_r 时,您将获得 stdClass 对象,但是当您尝试从该对象中使用 shortcode_title 时,您错误地调用它,因为它不是对象而是数组。

不正确:

   add_shortcode($each_shortcode['shortcode_title'],'shortcode_content');

正确的:

   add_shortcode($each_shortcode->shortcode_title,'shortcode_content');
于 2019-02-19T10:30:48.787 回答
0

您还需要在shortcode_add函数中获取短代码数据

这是完整的工作解决方案..

if(! function_exists('shortcode_content')){

    function shortcode_content( $atts, $content = null,$tag)    {
        global $wpdb;

        extract( shortcode_atts( array(
                    'shortcode_extra_data' => '', // available as $shortcode_extra_data
                ), $atts 
            ) 
        );

        $shortcode_value = $wpdb->get_var("SELECT shortcode_value FROM ds_shortcodes where shortcode_title = '".$tag."'");

        // do something with cotent
        // add shortcode_value with content provided in shortcode
        $content = $content.$shortcode_value;

        $content = apply_filters('the_content', $content); // use this according to your requirment, remove if not needed
        // return processed content
        return $content;

    }
}

/*
* call function on init hook
*/
add_action( 'init', 'shortcode_add' );
if(! function_exists('shortcode_add')){

    function shortcode_add(){
        global $wpdb;

        $all_shortcodes = $wpdb->get_results("SELECT shortcode_title, shortcode_value FROM ds_shortcodes", ARRAY_A);
        foreach($all_shortcodes as $short){
            add_shortcode($short['shortcode_title'],'shortcode_content');
        }

    }
}

和短代码调用

[short1 shortcode_extra_data="syzzzzzz"]something inside content[/short1]
于 2019-02-19T12:25:29.887 回答