Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress Actions Repeated Problems

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #253023
    hlaporthein
    Participant

    I have difficulties in using wordpress actions. I would like to send an email and do other stuff when user is viewing each single post.

    But, when I use wordpress <b>init or the_post</b> action to send an email, email is sending two times. I have also tested with <b>template_redirect</b> or other actions but it is still going two times.

    I googled it and saw this one Avoiding_infinite_loops. But, it doesn’t work for me. Please help me how I could be able to do <b> wordpress action repeat problem</b>.

    To understand what I said above. I created a simple code and it would create test.txt file in your theme directory and you could see each time we reload the page, it is saying twice in test.txt file. I also shared below codes on codeshare


    class ZB_API_WORKER { public function init() { add_action( 'the_post', array( $this, 'send_single_page_data' ), 100 ); } /** * @return bool * * @description Sending data when user view in single page */ public function send_single_page_data() { global $post; // remove_action( 'the_post', array( $this, 'send_single_page_data' ), 0 ); if ( is_single() ) { $this->unit_test(); } } public function send_mail() { $to = '[email protected]'; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); wp_mail( $to, $this->mail_subject, $this->mail_message, $headers ); } /** * @param $lorem_text_demo * * @description This is testing file write system */ public function unit_test( $lorem_text_demo = '' ) { $file_url = get_stylesheet_directory() . '/test.txt'; $file_write_text = fopen( $file_url, 'a' ) or die( 'unable to open it' ); $lorem_text = $lorem_text_demo; $process_box = date("h:i:sa") ."\n"."---------------------"; $file_write = fwrite( $file_write_text, "\n". $process_box ); fclose( $file_write_text ); } } $api_class = new ZB_API_WORKER(); $api_class->init();
Viewing 1 post (of 1 total)
  • The forum ‘Other’ is closed to new topics and replies.