0

Please I really need help. The code below is a code that saves search query as tag on my site, but it replaces the title with "-", Please is there any way i can make the tag title name remain as it is search ?.

function addsometags() {

//Don't do anything if we've already got 20 tags
$posttags = get_the_tags();
$count=0;
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if ($count==20) break;
}
}


if ($count<20) {
        global $wpdb;

        $engines['google.'] = 'q=';
        $engines['altavista.com'] = 'q=';
        $engines['search.msn.'] = 'q=';
        $engines['yahoo.'] = 'p=';
        $engines['bing.'] = 'q=';
        $engines['yandex.'] = 'text=';

        $referer = $_SERVER['HTTP_REFERER'];
        $blogtarget = $_SERVER["REQUEST_URI"];
        $ref_arr = parse_url("$referer");
        $ref_host = $ref_arr['host'];

        foreach($engines as $host => $skey){
            if (strpos($ref_host, $host) !== false){
                $res_query = urldecode($ref_arr['query']);
                if (preg_match("/{$engines[$host]}(.*?)&/si",$res_query."&",$matches)){
                    $query = trim($matches[1]);
                    $target = str_replace("'","''",str_replace(";","",sanitize_title_with_dashes($query)));
                    global $post;
                    $thePostID = $post->ID;
                    wp_add_post_tags($thePostID, $target);
            }
        }
    }
 }
}


add_action('wp_footer', 'addsometags');

Please Gurus Here, Help me out please, I am new to coding wp function

1
  • Why are you adding tags to a post on the frontend? You shouldn't do DB writes on the frontend it kills performance and fails if you have any kind of page cache
    – Tom J Nowell
    Jun 28 '18 at 18:53
0

wp_add_post_tags($thePostID, $target); assumes that $target is either the slug or ID of a tag. Not all possible values will be valid tag slugs, which is why the sanitize_title_with_dashes is used.

So instead, the real problem is that those tags have no human friendly name, just a slug.

So instead, fetch the term, use it's ID, and if it doesn't already exist, create it and use the new term ID

Fetch the term:

$name = str_replace("'","''",str_replace(";","",$query));
$slug = sanitize_title_with_dashes($name);
$term = get_term_by( 'slug', $slug, 'tag' );

If it wasn't found:

if ( false === $term ) {

Then create the tag:

    $term = wp_insert_term( $name, 'tag', [
        'slug' => $slug
    ]);

Finally, add it to your post:

}
wp_set_object_terms( $post_id, $term, 'tag', true );

Notice that I cut out the middle man by eliminating all the tag helper functions, and going straight for the term and taxonomy functions. Why learn a whole new set of functions for tags, then again for taxonomy, each behaving slightly differently, when you can learn 1 set of functions that work the same for everything

Performance and Functionality

This entire approach is bad for performance, scaling, and database load, because it makes changes on the frontend. This makes your entire site much slower.

Additionally, it makes page caching impossible, any page cache that gets added to this, or query caching, will cause this functionality to either fail or become very unreliable.

Instead, have you considered using Google Analytics or Jetpack stats which automatically track this information already?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.