Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用PHP在WordPress中动态包装字符串 问题:_Javascript_Php_Wordpress - Fatal编程技术网

Javascript 用PHP在WordPress中动态包装字符串 问题:

Javascript 用PHP在WordPress中动态包装字符串 问题:,javascript,php,wordpress,Javascript,Php,Wordpress,如何将®符号替换为®。更具体地说,在WordPress项目中,最干净的方法是什么 背景 我有一个Javascript解决方案,在我的 但我希望这种情况发生在服务器上,而不是客户端。在可湿性粉剂项目中,哪里是最好的标准位置?我只负责维护一个主题,所以把它放在那里,但具体在哪里 此外,该代码仅替换了第一个®。所以我需要循环一下 代码 Javascript 函数重新替换(){ var regStr=document.getElementById(“target div”).innerHTML;

如何将®符号替换为
®。更具体地说,在WordPress项目中,最干净的方法是什么

背景 我有一个Javascript解决方案,在我的

但我希望这种情况发生在服务器上,而不是客户端。在可湿性粉剂项目中,哪里是最好的标准位置?我只负责维护一个主题,所以把它放在那里,但具体在哪里

此外,该代码仅替换了第一个®。所以我需要循环一下


代码 Javascript
函数重新替换(){
var regStr=document.getElementById(“target div”).innerHTML;
var resSup=regStr.replace(“”,“®;”);
document.getElementById(“target div”).innerHTML=resSup;
}
重新替换();

最简单的方法是。它只需要一个过滤器函数,用另一个子字符串替换你的子字符串

全场替换 这将在所有用户生成的内容中使用所需的HTML替换所有出现的®

add_filter('the_content', 'replace_stuff');

function replace_stuff($content) {
    return str_replace("®", "<sup>&rep;</sup>", $content);
}
仅在一组特定页面上替换 与上面的基本相同,只是需要检查多个页面段塞

add_filter('the_content', 'maybe_replace_stuff');

function maybe_replace_stuff($content) {

    $acceptedSlugs = array('foo', 'bar');

    $post = get_post();
    $slug = $post->post_name;

    if (in_array($slug, $acceptedSlugs)) {
        $content = str_replace("®", "<sup>&rep;</sup>", $content);
    }

    return $content;
}
add_filter('u content','maybe_replace_stuff');
函数可能替换东西($content){
$acceptedSlugs=数组('foo','bar');
$post=get_post();
$slug=$post->post\u名称;
if(在_数组中($slug,$acceptedslug)){
$content=str_replace(“”,“&rep;”,$content);
}
返回$content;
}
仅当文章使用特定模板时才替换
add_filter('u content','maybe_replace_stuff');
函数可能替换东西($content){
$post=get_post();
$template=get_post_meta($post->ID,'wp_page_template',true);
如果($template==“your template.php”){
$content=str_replace(“”,“&rep;”,$content);
}
返回$content;
}

在:主题html中更改它?用户添加的内容?硬编码php?
str_replace(“”,“&rep;”,$string)?除非您在PHP中以不同的方式接收它?@nogad我在可能的地方硬编码了,但用户添加的一些内容是导致问题的原因。@Darren我相信您是对的。只是不知道该放在哪里。我有一个页面作为我正在处理的这些页面的模板。在
之间插入代码并将其放在页面顶部我可以将其添加到模板(对于这些页面)顶部吗?我想这对我不起作用,因为我没有正确添加它。
内容过滤器仅用于邮箱内容。不适用于侧边栏、菜单和小部件。显然,要替换模板文件中出现的内容,只需在您最喜欢的文本编辑器中进行项目范围的搜索,然后使用它即可。上面的代码段应该添加到functions.php文件中,以便在整个站点上运行。@ppajer这根本不是问题所在。我的意思是,该页面是一个模板,我上传到WP,该模板用于10个不同的项目,并在CMS中管理。由于内容是在CMS中添加的,这就是为什么我要问如何只管理一个重复使用的模板页面。@JGallardo对不起,我的评论的第一部分是对Nirjhar的回复,建议这在CMS未管理的地方不起作用-我有时忘记在触摸屏上标记人。:)所以你不需要替换所有帖子中的字符串,只需要替换特定的类别?如果是这样,我将不得不修改我的答案。
add_filter('the_content', 'maybe_replace_stuff');

function maybe_replace_stuff($content) {

    $post = get_post();
    $slug = $post->post_name;

    if ($slug === 'your-slug') {
        $content = str_replace("®", "<sup>&rep;</sup>", $content);
    }

    return $content;
}
add_filter('the_content', 'maybe_replace_stuff');

function maybe_replace_stuff($content) {

    $acceptedSlugs = array('foo', 'bar');

    $post = get_post();
    $slug = $post->post_name;

    if (in_array($slug, $acceptedSlugs)) {
        $content = str_replace("®", "<sup>&rep;</sup>", $content);
    }

    return $content;
}
add_filter('the_content', 'maybe_replace_stuff');

function maybe_replace_stuff($content) {

    $post = get_post();
    $template = get_post_meta($post->ID, '_wp_page_template', true);

    if ($template === 'your-template.php') {
        $content = str_replace("®", "<sup>&rep;</sup>", $content);
    }

    return $content;
}