wp_footer挂钩无法正常工作

时间:2015-11-03 16:24:56

标签: wordpress wordpress-plugin

我正在制作一个插件,在每个页面的右下角添加图片。 Followin是正常运行的代码,并将图像添加为内容。

add_filter('the_content', 'scroll_to_top_data');

但是我想在我试过的右下角的页脚之后添加它:

add_action('wp_footer', 'scroll_to_top_data'); 

但这不会在任何地方显示图像,也不会显示错误。我正在使用二十五个主题但当然我希望这个插件可用于wordpress的所有主题。请指导我为什么不使用wp_footer钩子,我怎么能在页脚后放这个?下面是scroll_to_top_data函数

function scroll_to_top_data($content = NULL) {
$post_id = get_the_ID();
global $wpdb;
$table = $wpdb->prefix . 'scroll';

$myrows = $wpdb->get_results("SELECT * FROM $table WHERE id = 1");
$beforeafter = $myrows[0]->beforeafter;
$where_like = $myrows[0]->where_like;
$status = $myrows[0]->status;
$image = $myrows[0]->image;
$action = $myrows[0]->action;
$color = $myrows[0]->color;
$display = $myrows[0]->display;
$except_ids = $myrows[0]->except_ids;
$url = $myrows[0]->url;
$width = $myrows[0]->width;
$position = $myrows[0]->position;
$str = $image;

if ($status != 0) {
    $scrollImage = '<img src="' . $image . '"';
    return $content . $scrollImage;
} else {
    return $content;
}
}

2 个答案:

答案 0 :(得分:1)

wp_footer操作未传递任何参数或期望任何返回值(如过滤器)(在本例中为$content)。过滤器希望传递信息以进行修改和返回,而操作则不会。要使图像输出,您必须更改以下内容:

if ($status != 0) {
    $scrollImage = '<img src="' . $image . '"';
    return $content . $scrollImage;
} else {
    return $content;
}

实际回显图像:

if ($status != 0) {
    echo '<img src="' . $image . '" />'; //doesn't seem to have a closing bracket?
}

而不是尝试将其附加到现有内容。

答案 1 :(得分:0)

可能是你的scroll_to_top_data永远不会被调用,特别是如果你在你的插件的构造函数中有它。您需要将其更改为 add_action(&#39; wp_footer&#39;,数组($ this,&#39; scroll_to_top_data&#39;));

看看这篇SO帖子: https://wordpress.stackexchange.com/questions/36013/remove-action-or-remove-filter-with-external-classes

相关问题