admin管理员组

文章数量:1125401

So far this is what I have... I'm new to developing plugins so I'm not sure how to make a plugin run code on a post save. I know that this would likely work if it was added to the post.php page but I want to do it as a plugin, eventually with a box of CSV on a settings page to create the array I manually defined here. The goal is to tag every post that names a city with that city's tag. Thank you in advance for any help.

<?php
/**
 * Plugin Name: Auto Post Tagger
 * Plugin URI: .zip
 * Description: The very 13th plugin that I have ever created.
 * Version: 1.10
 * Author: Cody King
 * Author URI: 
 */

/**
 * @snippet       Auto Post Tagger
 * @author        Cody King
 * @compatible    Wordpress 6.4.3
 */
  
  
function autotag( $post_id ) {
    foreach ($keywords as $keyword)
        {
            if(StrPos($post_id > the_content(),$keyword))
            {
                $keyword = str_replace(' ','-',$keyword);
                wp_set_post_tags($post_id,$keyword);
            }
        }
}

$keywords = ['Los Angeles','New York','Chicago','Houston','Phoenix']
add_action( 'save_post', 'autotag' );
?>

Edit #6: After some sleep and a clearer head I saw what was wrong with the syntax. This is the newest version below... It lets me activate it and has no error messages but it doesn't seem to do anything when I save posts or publish them. Am I missing a step? Do I need to run save_post in this function after I use wp_set_post_tags?

<?php
/**
 * Plugin Name: Auto Post Tagger
 * Plugin URI: .zip
 * Description: The very 13th plugin that I have ever created.
 * Version: 1.13
 * Author: Cody King
 * Author URI: 
 */

/**
 * @snippet       Auto Post Tagger
 * @author        Cody King
 * @compatible    Wordpress 6.4.3
 */


function autotag( $post_id ) {
    foreach ( $keywords as $keyword )
        {
            $keycheck = StrPos( get_the_content(), $keyword );
            if( $keycheck === true )
            {
                $keyword = str_replace( ' ' , '-' , $keyword ); // Convert spaces to dashes "-"
                wp_set_post_tags( $post_id , $keyword , true ); // Set tags to Post
            }
        }
}

$keywords = ['Los Angeles','New York','Chicago','Houston','Phoenix'];
add_action( 'save_post', 'autotag' );

So far this is what I have... I'm new to developing plugins so I'm not sure how to make a plugin run code on a post save. I know that this would likely work if it was added to the post.php page but I want to do it as a plugin, eventually with a box of CSV on a settings page to create the array I manually defined here. The goal is to tag every post that names a city with that city's tag. Thank you in advance for any help.

<?php
/**
 * Plugin Name: Auto Post Tagger
 * Plugin URI: http://www.sitecrafters.pro/plugins/auto-tagger.zip
 * Description: The very 13th plugin that I have ever created.
 * Version: 1.10
 * Author: Cody King
 * Author URI: http://sitecrafters.pro
 */

/**
 * @snippet       Auto Post Tagger
 * @author        Cody King
 * @compatible    Wordpress 6.4.3
 */
  
  
function autotag( $post_id ) {
    foreach ($keywords as $keyword)
        {
            if(StrPos($post_id > the_content(),$keyword))
            {
                $keyword = str_replace(' ','-',$keyword);
                wp_set_post_tags($post_id,$keyword);
            }
        }
}

$keywords = ['Los Angeles','New York','Chicago','Houston','Phoenix']
add_action( 'save_post', 'autotag' );
?>

Edit #6: After some sleep and a clearer head I saw what was wrong with the syntax. This is the newest version below... It lets me activate it and has no error messages but it doesn't seem to do anything when I save posts or publish them. Am I missing a step? Do I need to run save_post in this function after I use wp_set_post_tags?

<?php
/**
 * Plugin Name: Auto Post Tagger
 * Plugin URI: http://www.sitecrafters.pro/plugins/auto-tagger.zip
 * Description: The very 13th plugin that I have ever created.
 * Version: 1.13
 * Author: Cody King
 * Author URI: http://sitecrafters.pro
 */

/**
 * @snippet       Auto Post Tagger
 * @author        Cody King
 * @compatible    Wordpress 6.4.3
 */


function autotag( $post_id ) {
    foreach ( $keywords as $keyword )
        {
            $keycheck = StrPos( get_the_content(), $keyword );
            if( $keycheck === true )
            {
                $keyword = str_replace( ' ' , '-' , $keyword ); // Convert spaces to dashes "-"
                wp_set_post_tags( $post_id , $keyword , true ); // Set tags to Post
            }
        }
}

$keywords = ['Los Angeles','New York','Chicago','Houston','Phoenix'];
add_action( 'save_post', 'autotag' );
Share Improve this question edited Feb 7, 2024 at 15:13 Cody King asked Feb 6, 2024 at 15:09 Cody KingCody King 214 bronze badges 9
  • the_content does not return the posts content, it echo's it out so you can't use it to do if statements or checks, take a look at get_the_content. Likewise it doesn't know which post to get the content for so it might fetch the wrong one.You also don't need it, the post being saved is the second argument of that action, see: developer.wordpress.org/reference/hooks/save_post – Tom J Nowell Commented Feb 6, 2024 at 15:20
  • THanks. I get what you mean and appreciate the nudge in the right direction. Can you tell me how to activate this function with a plugin? The error it's throwing at me is saying that "add_action" is causing the problem. If so, what do I use to call it without editing my theme files? – Cody King Commented Feb 6, 2024 at 15:53
  • Can you include the error message in your question? I expect it’s unlikely to be related to plugins and more likely to be syntax errors. Are you writing this code yourself or is AI helping you? – Tom J Nowell Commented Feb 6, 2024 at 19:06
  • Now it is allowing me to activate the plugin but it is saying "returned 2 unexpected characters" when I activate it.. and it doesn't seem to do anything. I realized I had it backwards and corrected the syntax but no luck. – Cody King Commented Feb 6, 2024 at 20:34
  • Remove the ?> at the end of the code -- it's not needed. It's possible there are whitespace characters like tabs or spaces after the ?> which might cause the "unexpected characters" warning/error. – Pat J Commented Feb 6, 2024 at 20:36
 |  Show 4 more comments

2 Answers 2

Reset to default 1

After the advice from every awesome person who helped, I finally got it to work. I just had to define $the_content instead of trying to use the hook get_the_content() alone. Now it's tagging everything perfectly. Here is the final code for anyone who wants to use it in the future. Or you can just download my plugin when I'm finished making it. It will be free.

<?php
/**
 * Plugin Name: Auto Post Tagger
 * Plugin URI: http://www.sitecrafters.pro/plugins/auto-tagger.zip
 * Description: The very 13th plugin that I have ever created.
 * Version: 1.15
 * Author: Cody King
 * Author URI: http://sitecrafters.pro
 */

/**
 * @snippet       Auto Post Tagger
 * @author        Cody King
 * @compatible    Wordpress 6.4.3
 */


function autotag( $post_id ) {
    $keywords = ['Los Angeles','New York','Chicago','Houston','Phoenix'];
    $the_content = apply_filters('the_content', get_the_content());
    foreach ( $keywords as $keyword )
        {
            $keycheck = strpos( $the_content, $keyword );
            if( $keycheck !== false )
            {
                $keyword = str_replace( ' ' , '-' , $keyword ); // Convert spaces to dashes "-"
                wp_set_post_tags( $post_id , $keyword , true ); // Set tags to Post
            }
        }
}

add_action( 'save_post', 'autotag' );

As per my comment, I suspect what's happening is that you have your $keywords array outside the function. So when the function runs the foreach() it has nothing to iterate through, so does nothing.

Try putting the $keywords inside the function before the foreach().

<?php
/**
 * Plugin Name: Auto Post Tagger
 * Plugin URI: http://www.sitecrafters.pro/plugins/auto-tagger.zip
 * Description: The very 13th plugin that I have ever created.
 * Version: 1.13
 * Author: Cody King
 * Author URI: http://sitecrafters.pro
 */

/**
 * @snippet       Auto Post Tagger
 * @author        Cody King
 * @compatible    Wordpress 6.4.3
 */


function ck_autotag_save( $post_id ) {
    if( !current_user_can( 'edit_post', $post_id ) ) :
        return $post_id;
    endif;
    $the_post = get_post( $post_id );
    $keywords = ['Los Angeles','New York','Chicago','Houston','Phoenix'];
    foreach( $keywords as $keyword ) :
        $keycheck = strpos( $the_post->post_content, $keyword );
        if( $keycheck === true ) :
            $keyword = str_replace( ' ' , '-' , $keyword ); // Convert spaces to dashes "-"
            wp_set_post_tags( $post_id , $keyword , true ); // Set tags to Post
        endif;
    endforeach;
}
add_action( 'save_post', 'ck_autotag_save' );

I think that if you're having an error it's most likely that the issue is with $keycheck not returning true. Is it possible that having StrPos in camelcase is the issue? Try running it as strpos() instead. Also, I don't think your get_post_content() is returning anything. I just tested it in a similar context and got nothing. When I tried to add just a post ID, that didn't fix it. I've re-written that segment above.

The function to save the tags to the post when the post is saved looks correct. So I think the most likely issue is that your $keycheck is returning false and I've added a few things to solve that.

本文标签: hooksHow to run this plugin every time a post is saved