我正在写我的第一个wordpress插件。每当用户保存/编辑页面时,它应该在插件页面上发送消息。问题是我无法打印消息。
这是我的代码:
<?php
/*
Plugin Name: Monitor
Plugin URI:
Description: Test plugin monitoring user activities
Author:
Version: 1.0
Author URI:
*/
if (!function_exists('p_update')) {
function p_update( $post_id ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$message = "Changed: " . $post_title . "Link: " . $post_url ;
echo "<p>test1</p>"
}
}
if (!function_exists('p_publish')) {
function p_publish( $post_id ){
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$message = "abc";
echo "<p>test2</p>"
}
}
if (!function_exists('plugin_menu')) {
function plugin_menu() {
add_menu_page( 'Monitor', 'Monitor', 'manage_options', __FILE__, 'plugin_options');
}
}
if (!function_exists('plugin_options')) {
function plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<p>test</p>';
echo '</div>';
}
}
add_action( 'admin_menu', 'plugin_menu' );
add_action('save_post', p_update);
add_action('publish_post', p_publish);
?>
plugin_menu函数正确显示html,但其他函数不能这样做。我究竟做错了什么?我确信有一些简单的方法可以做到这一点,但我见过的任何教程都没有解决这个问题。
提前感谢您的帮助。
答案 0 :(得分:0)
您应该这样写add_actions
:
add_action('save_post', 'p_update');
add_action('publish_post', 'p_publish');
您应在add_action
函数中使用''作为函数名称