如何在 WordPress 中将数据库 table 中的每条记录注册为简码
How can I register in WordPress every record from a database table as a shortcode
我需要一些帮助,希望你能帮助我。
我是一名初学者 WordPress 开发人员,我正在尝试开发一个短代码插件,它允许我随时在 WP Admin 中添加新的短代码并在前端显示它们。
我没有使用 WP post 类型。我创建了一个单独的 table,只有 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 天的尝试,非常感谢您的帮助。
谢谢。
阿林
您是否注意到,当您执行 print_r 时,您正在获取 stdClass 对象,但是当您尝试从该对象使用 shortcode_title 时,您错误地调用了它,因为它不是一个对象但是一个数组。
不正确:
add_shortcode($each_shortcode['shortcode_title'],'shortcode_content');
正确:
add_shortcode($each_shortcode->shortcode_title,'shortcode_content');
您还需要在 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]
我需要一些帮助,希望你能帮助我。
我是一名初学者 WordPress 开发人员,我正在尝试开发一个短代码插件,它允许我随时在 WP Admin 中添加新的短代码并在前端显示它们。
我没有使用 WP post 类型。我创建了一个单独的 table,只有 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 天的尝试,非常感谢您的帮助。
谢谢。
阿林
您是否注意到,当您执行 print_r 时,您正在获取 stdClass 对象,但是当您尝试从该对象使用 shortcode_title 时,您错误地调用了它,因为它不是一个对象但是一个数组。
不正确:
add_shortcode($each_shortcode['shortcode_title'],'shortcode_content');
正确:
add_shortcode($each_shortcode->shortcode_title,'shortcode_content');
您还需要在 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]