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
|
Show 4 more comments
2 Answers
Reset to default 1After 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
版权声明:本文标题:hooks - How to run this plugin every time a post is saved? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736659422a1946341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_content
does not return the posts content, itecho
's it out so you can't use it to doif
statements or checks, take a look atget_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?>
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