如何在WordPress中将数据库表中的每条记录注册为短代码

问题描述 投票:1回答:2

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

我是初学者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中为我在admin中添加的每个短代码手动复制此函数。

我试过这个:

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天的尝试,我真的很感谢你的帮助。

谢谢。

阿林

php wordpress shortcode wordpress-shortcode
2个回答
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]

0
投票

您是否注意到当您正在执行print_r以便获取stdClass对象时,但是当您尝试使用此对象中的shortcode_title时,您错误地将其称为因为它不是对象而是数组。

不正确:

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

正确:

   add_shortcode($each_shortcode->shortcode_title,'shortcode_content');
© www.soinside.com 2019 - 2024. All rights reserved.