我试图在提交“type:pelicula”帖子之前发送一封电子邮件,但它没有进入功能post_published_notification, 代码如下:
<?php
/**
* Plugin Name: VRlife Alertas Publicaciones
* Description: Avisa por el correo configurado al publicar un nuevo video
* Version: 1.0.0
* Author: José Manuel Lascasas
**/
//1
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
//2
add_action( \'admin_menu\', array( $this, \'add_plugin_page\' ) );
//6
add_action( \'admin_init\', array( $this, \'page_init\' ) );
add_action( \'publish_pelicula\', \'post_published_notification\', 10, 2 );
}
/**
* Add options page
*/
//3
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
\'Settings Admin\',
\'My Settings\',
\'manage_options\',
\'my-setting-admin\',
//4
array( $this, \'create_admin_page\' )
);
}
/**
* Options page callback
*/
//5
public function create_admin_page()
{
// Set class property
$this->options = get_option( \'my_option_name\' );
?>
<div class="wrap">
<h1>My Settings</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( \'my_option_group\' );
do_settings_sections( \'my-setting-admin\' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
//7
public function page_init()
{
register_setting(
\'my_option_group\', // Option group
\'my_option_name\', // Option name
//8
array( $this, \'sanitize\' ) // Sanitize
);
//10
add_settings_section(
\'setting_section_id\', // ID
\'My Custom Settings\', // Title
//11
array( $this, \'print_section_info\' ), // Callback
\'my-setting-admin\' // Page
);
//13
add_settings_field(
\'title\',
\'Mails\',
//14
array( $this, \'title_callback\' ),
\'my-setting-admin\',
\'setting_section_id\'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
//9
public function sanitize( $input )
{
$new_input = array();
if( isset( $input[\'title\'] ) )
$new_input[\'title\'] = sanitize_text_field( $input[\'title\'] );
return $new_input;
}
/**
* Print the Section text
*/
//12
public function print_section_info()
{
print \'Enter your settings below:\';
}
/**
* Get the settings option array and print one of its values
*/
//15
public function title_callback()
{
printf(
\'<input type="text" id="title" name="my_option_name[title]" value="%s" />\',
isset( $this->options[\'title\'] ) ? esc_attr( $this->options[\'title\']) : \'\'
);
$maill=$this->options[\'title\'];
//wp_mail( $maill, "wdaawddwwa", "Easy", "" );
}
public function post_published_notification( $ID, $post ) {
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( \'display_name\', $author );
$email = get_the_author_meta( \'user_email\', $author );
$title = $post->post_title;
$permalink = get_permalink( $ID );
$edit = get_edit_post_link( $ID, \'\' );
$to[] = $this->options[\'title\'];
$subject = sprintf( \'Published: %s\', $title );
$message = sprintf (\'Congratulations, %s! Your article “%s” has been published.\' . "\\n\\n", $name, $title );
$message .= sprintf( \'View: %s\', $permalink );
$headers[] = \'\';
var_dump("hola");
wp_mail( $to, "wdwadwad", "dwwdawddw", "" );
}
}
//16
$my_settings_page = new MySettingsPage();
SO网友:Jacob Peattie
好吧,让我们将钩子与正在工作的钩子进行比较:
add_action( \'admin_init\', array( $this, \'page_init\' ) );
add_action( \'publish_pelicula\', \'post_published_notification\', 10, 2 );
page_init()
和
post_published_notification()
都是
MySettingsPage
类,但您为每个类设置了不同的操作回调。
的第二个参数add_action()
是一个callback. 它告诉WordPress/PHP在触发操作时运行哪个函数。对于调用类方法的操作,需要传递一个数组:
实例化对象的方法作为数组传递,该数组包含索引0处的对象和索引1处的方法名称。
既然你在跑步add_action
在类中,所讨论的对象是$this
, 方法名称为post_published_notification
. 您已为正确执行此操作page_init
, 所以你只需要对post_published_notification
:
add_action( \'publish_pelicula\', array( $this, \'post_published_notification\' ), 10, 2 );