替换woocommerce的产品标题的功能/过滤器/动作


Function/filter/action to replace product title for woocommerce

我有这个函数

function my_product_title($title, $id)
    {
if(in_the_loop() && is_product())
{
    return '<span class="border">FooBar</span>';
}
return $title;
}
add_filter( 'the_title', 'my_product_title', 5, 2);

,可以用return '<span class="border">FooBar</span>';代替产品名称。

我也有一个自定义脚本在"mycustomtitle.php",可以修改产品的标题,我的脚本可以回显修改后的标题为$mycustomtitle

我想用我的$mycustomtitle代替原来的产品标题,而不改变核心文件中的任何内容。

我试着把return '<span class="border">FooBar</span>';改成$mycustomtitle,但它只删除了原来的标题,根本没有输出…

谢谢!

UPDATE 2016-10-20自定义代码:

<?php
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';
$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php'); 
$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];
if(!function_exists(spin11)){
function spin11($string11) {
    while(true) {
        if(!preg_match_all('/({([^'{]*?)'})/', $string11, $matches))
            break;
        foreach($matches[2] as $i => $match) {
            $parts = explode('|', $match);
            $string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
        }
    }
    return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
    $str = explode($from,$str,2);
    return $str[0].$to.$str[1];
}  
$title_id11  = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
?>

在函数中插入自定义代码。

function my_product_title($title, $id) {
    include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';
    $tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php'); 
    $tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
    $title_keys = array($tit1,$tit2);
    $title_key11 = $title_keys[array_rand($title_keys)];
    if(!function_exists(spin11)){
        function spin11($string11) {
            while(true) {
                if(!preg_match_all('/({([^'{]*?)'})/', $string11, $matches))
                    break;
                foreach($matches[2] as $i => $match) {
                    $parts = explode('|', $match);
                    $string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
                }
            }
            return $string11;
        }
    }
    if(!function_exists(str_replace_once11)){
        function str_replace_once11($from,$to,$str)
        {
            $str = explode($from,$str,2);
            return $str[0].$to.$str[1];
        }  
        $title_id11  = get_the_ID();
        $fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
        if(!file_exists($fileLocation11)){
            $file11 = fopen($fileLocation11,"w");
            $content11 = spin11($title_key11);
            fwrite($file11,$content11);
            fclose($file11);
        }
        if(file_exists($fileLocation11)){
            $myFile11 = $fileLocation11;
            $fh11 = fopen($myFile11, 'r');
            $theData11 = fread($fh11, filesize($myFile11));
            fclose($fh11);
        }
    }
    if(in_the_loop() && is_product()) {
        return $theData11;
    }
    return $title;
}
add_filter( 'the_title', 'my_product_title', 5, 2);

$mycustomtitle是全局变量还是在另一个函数/方法/类中定义?如果它是全局变量,这可能会有帮助:

function my_product_title($title, $id)
{
      global $mycustomtitle;
      if(in_the_loop() && is_product())
      {
        return $mycustomtitle;
      }
      return $title;
}
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/seo-engine/explode.php';
$tit1 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php'); 
$tit2 = $boomprint[array_rand($boomprint)].' '.file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/seo-controlpanel/write-to/product-title-h1.php');
$title_keys = array($tit1,$tit2);
$title_key11 = $title_keys[array_rand($title_keys)];
if(!function_exists(spin11)){
function spin11($string11) {
    while(true) {
        if(!preg_match_all('/({([^'{]*?)'})/', $string11, $matches))
            break;
        foreach($matches[2] as $i => $match) {
            $parts = explode('|', $match);
            $string11 = str_replace_once11($matches[0][$i], $parts[mt_rand(0, count($parts)-1)], $string11);
        }
    }
    return $string11;
}
}
if(!function_exists(str_replace_once11)){
function str_replace_once11($from,$to,$str)
{
    $str = explode($from,$str,2);
    return $str[0].$to.$str[1];
}  
$title_id11  = get_the_ID();
$fileLocation11 = getenv("DOCUMENT_ROOT") . '/wp-content/plugins/seo-controlpanel/seo-cache/product-title-h1/'.$title_id11.'.txt';
if(!file_exists($fileLocation11)){
$file11 = fopen($fileLocation11,"w");
$content11 = spin11($title_key11);
fwrite($file11,$content11);
fclose($file11);
}
if(file_exists($fileLocation11)){
$myFile11 = $fileLocation11;
$fh11 = fopen($myFile11, 'r');
$theData11 = fread($fh11, filesize($myFile11));
fclose($fh11);
}
}
//echo $theData11;
?>