0

I did a plugin to create a when the cookie is FALSE. Actually, the was working, but when I was about to create the cookie, it said that my header was already sent.

Then, I had the code add_action('init', 'newsletterSuscriber'); but it still returned an error like this one.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'newsletterSuscriber' not found or invalid function name in /PATH/wp-includes/plugin.php on line 503

Plugin code

    class Okidoo_Newsletter_Suscriber {

    /**
    * Initializes the plugin.
    *
    * To keep the initialization fast, only add filter and action in the constructor.
    */

    public function __construct() {

        add_shortcode( 'custom-suscribe-form' , array( $this, 'render_suscribe_form') );

        add_action( 'init', 'newsletterSuscriber' );
    }



    /**
    * A shortcode for rendering the login form.
    *
    * @param    array   $attributes     Shortcode attributes.
    * @param    array   $content        The text content for shortcode. Not used.
    * 
    * @return   string                  The shortcode output
    */

    public function render_suscribe_form( $attributes, $content = null ) {

        // Parse shortcode attributes
        $default_attributes = array( 'show_title' => false );
        $attributes = shortcode_atts( $default_attributes, $attributes );
        $show_title = $attributes['show_title'];

        // Pass the redirect parameter to the WordPress login functionnality : by default,
        // don't specify a redirect, but if a valid redirect URL has been passed as 
        // request parameter, use it.
        $attributes['redirect'] = '';

        // Render the login form using an external template.
        return $this->get_template_html( 'subscription_form', $attributes );
    }


    /**
    * Render the contents of the given template to a string and returns it.
    * @param    string  $template_name  The name of the template to render (without .php)
    * @param    array   $attributes     The PHP variables for the template
    *
    * @return   string                  The contents of the template.
    */
    private function get_template_html( $template_name, $attributes = null ) {
        if ( ! $attributes ) {
            $attributes = array();
        }

        ob_start();

        do_action( 'personalize_div_before_' . $template_name );

        require( 'templates/' . $template_name . '.php' );

        do_action( 'personalize_div_before_' . $template_name );

        $html = ob_get_contents();

        ob_end_clean();

        return $html;
    }

    public function newsletterSuscriber() {
        if ( ! isset($_COOKIE['newsletter_suscriber'])) {

            $path = parse_url(get_option('siteurl'), PHP_URL_PATH);
            $host = parse_url(get_option('siteurl'), PHP_URL_HOST);
            $expiration = time() + 3600;

            setcookie('newsletter_suscriber', 'newsletter_suscriber', $expiration, $path, $host);
        }
    }

}

// Initialize the plugin
$initialisation = new Okidoo_Newsletter_Suscriber();

Afer this, I have try to place the add_action on mey functions.php file in the template and also in the plugin file directly.

Any one can tell me if it is possible to :

  1. Execute a plugin with the init action;
  2. What I am doing wrong if point #1 is true?

Thx

2

It errors because class method is not a function. It's not uniquely identified by its name alone. You need to provide instance of the object (or name of the class for static methods).

In your context it would be:

add_action( 'init', [ $this, 'newsletterSuscriber' ] );
  • That thing did the job I wanted to do. I will try to read more about your explination on the Codex. Meanwhile, can you tell me if it is the good way to do so? – Patrice Poliquin Sep 2 '15 at 20:36
  • Not sure what you are asking precisely. :) Nothing jumps out at me as horribly wrong with your code, but I hadn't looked closely beyond your issue. – Rarst Sep 2 '15 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.