首页 > 解决方案 > Wordpress - 如果 function_exists 使用 Front End PM 插件向用户发送消息

问题描述

我正在使用插件“前端 PM ”,并且我有下面的代码片段,用于在发布作者帖子时向前端发送消息,但对我不起作用并且我得到“意外”(T_STRING)“的错误,所以[''] 之间的问题,但很抱歉我无法解决。

add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

function fep_cus_user_publish_send_messaage( $ID, $post ){

    if ( ! function_exists( 'fep_send_message' ) )
    return;
    $message = [];
 
    $message['message_to_id'] = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $post->post_author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID ); 
    $message['message_title'] = sprintf( 'Published: %s', $title );
    $message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
    $message['message_content'] .= sprintf( 'View: %s', $permalink );

    $override = array('post_author' => 1, //change with message sender id
        
    );

    // Send message
    fep_send_message( $message, $override );      
}

标签: phpwordpressemailauthor

解决方案


在适当的文本编辑器中编辑您的代码片段,例如sublime textnotepad++Vim(仅举几例)。在某些行后面,您有从站点的格式不正确的代码块或不是用于编写代码的工具复制的字符。如果您删除这些字符,您的代码将不会产生语法错误。

像这样的片段应该可以工作:

add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

function fep_cus_user_publish_send_messaage( $ID, $post ){

    if ( ! function_exists( 'fep_send_message' ) )
    return;
    $message = [];
 
    $message['message_to_id'] = $post->post_author; // Post author ID. 
    $name = get_the_author_meta( 'display_name', $post->post_author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID ); 
    $message['message_title'] = sprintf( 'Published: %s', $title );
    $message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.' . '\n\n', $name, $title );
    $message['message_content'] .= sprintf( 'View: %s', $permalink );
    $override = array('mgs_author' => 1);//change with message sender id  
    

    // Send message
    fep_send_message( $message, $override );      
}

编辑: post_author 已被弃用并删除。改为使用mgs_author


推荐阅读