最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

actions - add_action( 'publish_pelicula', 'post_published_notification', 10, 2 ); does not trigg

matteradmin8PV0评论

I'm trying to send an Email before a post "type:pelicula" is submited, but it does not enter to the function post_published_notification, here is the code:

<?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();

I'm trying to send an Email before a post "type:pelicula" is submited, but it does not enter to the function post_published_notification, here is the code:

<?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();
Share Improve this question edited Mar 7, 2019 at 11:37 Jacob Peattie 44.3k10 gold badges50 silver badges64 bronze badges asked Mar 7, 2019 at 11:28 Jose Manuel Lascasas JimenezJose Manuel Lascasas Jimenez 318 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Well let's compare the hook to one that is working:

add_action( 'admin_init', array( $this, 'page_init' ) );
add_action( 'publish_pelicula', 'post_published_notification', 10, 2 );

page_init() and post_published_notification() are both methods of the MySettingsPage class, but you've set the action callbacks for each of them differently.

The second argument for add_action() is a callback. It tells WordPress/PHP which function to run when the action is fired. For an action to call a class method, you need to pass an array:

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

Since you're running add_action inside a class, the object in question is $this, and the method name is post_published_notification. You've done this correctly for page_init, so you just need to do that same for post_published_notification:

add_action( 'publish_pelicula', array( $this, 'post_published_notification' ), 10, 2 );

Since you are in a class, you need to specify that the function you want ("post_published_notification") is a part of that class.

You do this by specifying the class before the function as part of an array. It would look like this:

add_action( 'publish_pelicula', array( $this, 'post_published_notification' ), 10, 2 );

Post a comment

comment list (0)

  1. No comments so far