嗨~我又回来了,我正在尝试为我正在编写的脚本添加另一个函数a plugin that notify my friends of new post that mentions(@) them. 现在,我想在点击publish后,将我提到的朋友姓名的纯文本更改为他们自己的wordpress博客地址(如果有)的链接。所以@David将成为David.
我多次搜索此问题,但所有结果如下:
add_filter(\'the_content\', \'replace_custom_word\');
这并不能真正修改数据库,对吗?编辑:我不知道,也许我错了。
下面是我用来获取David博客地址的代码。
$friend_url = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author_url
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friendCorrectName
)) ;
所以请告诉我最好的方法是什么。提前多谢!
此脚本的完整代码也粘贴在下面,请随时对我现在的内容发表评论。:)你可以查看我的last post 如果你看到什么奇怪的东西,或者你可以问我。
function email_friend() {
// get post object
$post = get_post($id);
// get post content
$content = $post->post_content;
// get how many people is mentioned in this post
$mentionCount = preg_match_all(\'/(@[^\\s]+)/\', $content, $matches);
// if there is at least one @ with a name after it
if (0 !== $mentionCount) {
$friendList = array();//for storing correct names
for ($mentionIndex=0; $mentionIndex < $mentionCount; $mentionIndex++) {
$mentionName = $matches[0][$mentionIndex];
$mentionName = str_replace(\'_\',\' \',$mentionName); //change _ back to space
$mentionName = substr($mentionName, 1); //get rid of @
//for security and add wildcard
$friend_display_name_like = \'%\' . like_escape($mentionName) . \'%\';
global $wpdb;
// get correct name first
$friendCorrectName = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friend_display_name_like
)) ;
// get friend email by comment author name
$friend_email = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author_email
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friendCorrectName
)) ;
// get friend\'s blog address
$friend_url = $wpdb->get_var( $wpdb->prepare( "
SELECT comment_author_url
FROM $wpdb->comments
WHERE comment_author
LIKE %s ",
$friendCorrectName
)) ;
//if we have David\'s blog address in database
if ($friend_url) {
//this is where I need help with.
//I need to modify post content before writing it into database now
//I need to change the plain text name after @ to a link to his blog
}
if($friend_email) {// if found email address then email
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = $friend_email;
$subject = \'Arch!tect mentioned you in his new post 《\'.$postTitle .
\'》\';
$from = "[email protected]";
$headers = "From:" . $from;
$message = "Arch!tect mentioned you in his new post《".$postTitle .
"》 check it out?\\n\\n" ."Post link:".$post_permalink
."\\n\\n\\nPlease don\'t reply this email.\\r\\n";
if(mail($to, $subject, $message, $headers)) {
//if send successfully put his/her name in my list
array_push($friendList, $friendCorrectName);
}
}
}
$comma_separated_list = implode(",", $friendList); //friend list array to string
// now send an email to myself about the result
$postTitle = get_the_title($id);
$post_permalink = get_permalink( $id );
$to = \'[email protected]\';
$subject = "Your new post《".$postTitle .
"》has notified ".count($friendList)."friends successfully";
$from = "[email protected]";
$headers = "From:" . $from;
//list all friends that received my email
$message = "Your new post《".$postTitle .
"》has notified ".count($friendList)."friends successfully:\\n\\n".
$comma_separated_list;
mail($to, $subject, $message, $headers);
}
}//end of email_friend function
add_action ( \'publish_post\', \'email_friend\' );
EDIT2: to be more specific, the code above where I comment " //this is where I need help with. " is the place I need help with.
最合适的回答,由SO网友:RRikesh 整理而成
您通常使用add_filter
在显示信息之前。对于您的情况,您可以使用add_action(\'publish_post\', \'your_function\')
然后你拦截$_POST
价值观所见即所得编辑器的内容可通过$_POST[\'content\']
.
吊钩为publish_{post_type}
顺便说一句,你应该做出相应的改变。
示例:
function wpse_89292_save_my_post()
{
print_r( $_POST[\'content\'] ); # will display contents of the default wysiwyg editor
die;
}
add_action(\'publish_post\', \'wpse_89292_save_my_post\');
使用
wp_insert_post_data
过滤器:
function wpse_89292_save_my_post( $content ) {
global $post;
if( isset($post) && get_post_type( $post->ID ) == \'post\' ){
$content[\'post_content\'] = function_to_manipulate_the_content();
}
return $content;
}
add_filter( \'wp_insert_post_data\', \'wpse_89292_save_my_posts\' );