Php 简单的短代码问题,为傻瓜编码
我被要求帮助一个朋友在Wordpress网站上显示一些自定义数据,很简单,我想 有一个包含客户数据的表。插件的想法是根据当前用户id查询表,并将其作为短代码输出,这样他们就可以将输出放在任何他们喜欢的地方 在谷歌搜索了很多次之后(我不是一个程序员),我得到了一些有用的东西!问题是,即使在我未经训练的眼睛看来,它看起来像一个傻瓜,当有一个更聪明的方法来做的时候,它会重复询问。。。。请聪明的人根据这个>>给我指路好吗Php 简单的短代码问题,为傻瓜编码,php,mysql,wordpress,Php,Mysql,Wordpress,我被要求帮助一个朋友在Wordpress网站上显示一些自定义数据,很简单,我想 有一个包含客户数据的表。插件的想法是根据当前用户id查询表,并将其作为短代码输出,这样他们就可以将输出放在任何他们喜欢的地方 在谷歌搜索了很多次之后(我不是一个程序员),我得到了一些有用的东西!问题是,即使在我未经训练的眼睛看来,它看起来像一个傻瓜,当有一个更聪明的方法来做的时候,它会重复询问。。。。请聪明的人根据这个>>给我指路好吗 <?php Plugin Name: do stuff Desc
<?php
Plugin Name: do stuff
Description: Do site specific stuff
function spend_data() {
global $wpdb;
global $current_user;
wp_get_current_user();
$userid = $current_user->ID;
$spend = $wpdb->get_var ( "SELECT spend FROM mytable WHERE the_id=$userid");
return $spend;
}
function detail_data() {
global $wpdb;
global $current_user;
wp_get_current_user();
$userid = $current_user->ID;
$detail = $wpdb->get_var ( "SELECT detail FROM mytable WHERE the_id=$userid");
return $programs;
}
add_shortcode('spend', 'spend_data');
add_shortcode('detail', 'detail_data');
/* Stop Adding Functions Below this Line */
我想您可以在短代码之前触发的钩子上查询所有字段,然后将其全部存储在$current\u user
中。不要忘记用正确的表名替换mytable
function marks_prepare_user_shortcodes () {
global $wpdb;
global $current_user;
$current_user = wp_get_current_user();
$userid = $current_user->ID;
$row = $wpdb->get_row( "SELECT * FROM mytable WHERE the_id=".$userid, ARRAY_A);
$current_user->marks_shortcodes = $row;
}
add_action( 'wp_head', 'marks_prepare_user_shortcodes');
然后你的短代码变为
function spend_data() {
global $current_user;
$spend = $current_user->marks_shortcodes['spend'];
if (!is_null($spend))
return $spend;
}
function detail_data() {
global $current_user;
$detail = $current_user->marks_shortcodes['detail'];
if (!is_null($detail))
return $detail;
}
add_shortcode('spend', 'spend_data');
add_shortcode('detail', 'detail_data');
我不确定这里选择的wp\u head
是否正确/最好,您可以试试。我想它是在所有的短代码被调用之前被调用的。代码未经测试
另外,作为旁注,我认为您的额外用户数据实际上应该存储在wp_usermeta
表中,而不是单独的表中。您可以使用get\u user\u meta()
检索字段:
更新
除了上面的代码块,您还可以使用:
function mikes_data_shortcode($atts) {
global $current_user;
$atts = array_change_key_case((array)$atts, CASE_LOWER);
// override default attributes with user attributes
$actual_atts = shortcode_atts([
'field' => 'YouDidntEnterAFieldNameAsAttribute',
], $atts);
$data = $current_user->marks_shortcodes[$actual_atts['field']];
if (!is_null($data))
return $data;
}
function mikes_shortcodes_init()
{
add_shortcode('mikes_data_shortcode', 'mikes_data_shortcode');
}
add_action('init', 'mikes_shortcodes_init');
使用以下短代码时:
[mikes\u data\u shortcode field=“spend”]
或
[mikes\u data\u shortcode field=“detail”]
调用时,函数将返回一个值,在您的情况下,该值是指用户当前已花费的mony金额。(等等。最后一行代码告诉你,我们必须在你的页面中添加这些信息,以便向登录用户显示这些信息。嗨,马克,欢迎这么做。如果只有这两个短代码,我觉得这看起来还不错。你有性能问题吗?谢谢Snowerd!这将超过两个,可能是10/12。我确信服务器将正常运行vive,它似乎不是很优雅?可能吧,但我忍不住觉得它看起来更像这样?函数all_the_data(){$stuff=$wpdb->get_results(“SELECT*FROM mytable WHERE the_id=$userid”);return$stuff;}添加_shortcode('stuff','all_the_data');
页面快捷代码看起来像>[stuff'spend'][内容“细节”]在短代码中有一个查询和参数?关于第二部分,你是对的,实际上我想为短代码创建参数,但我认为它超出了范围。等一下。添加了请求字段作为短代码中的参数的方法。我将其改为echo
,而不是return
顺便说一句。对不起,pi孔被删除了e因此选择了葡萄酒而不是网站:)将数据放在一个单独的表中,因为我不确定它将如何导入/更新,认为远离核心WP表更安全!看起来你的答案就是我想要的,只是现在要说服WP玩球,而不是发出“警告:尝试分配非对象的属性“marks_shortcode”和“Undefined variable:tag”在“&非法字符串偏移量”中花费“&”未初始化字符串偏移量:0 in”等etcI中,我刚刚在mo中得到上述错误,稍后将尝试找出代码的工作方式,看看是否可以修复它。