0

I am working of a simple redirect plugin that match the given slug and redirect to the assigned url, which i supposed to working fine.

But recently i am adding the functionality to update the stat for the visited slug and i found that my update query called twice, i checked and found that template_redirect hook called twice. My stat update query is applied on single time only. Here is my code:

add_action("template_redirect", array($this, "redirection_section"), 11);

function redirection_section() {
    if( is_404() )
        $this->uni_page_redirect();
}

function uni_page_redirect() {

        include_once dirname(__FILE__) . '/uni_links.php';
        $Links = new UNI_Links();

        $slug = ltrim(preg_replace('#/$#', '', urldecode($_SERVER['REQUEST_URI'])), '/');


        if( $Links->is_valid_slug($slug) ) {
            // Initiate with Slug
            do_action("ulr_init_slug", $slug);
            $Links->generate_links($slug);
            exit();
        }

        return false;

    }

Now this is how i am updating my query:

function slug_init_callback($slug_name) {
        global $wpdb;

        $table_slugs = ULR_TABLE_SLUGS;

        // Debug to check invoks
        file_put_contents(PLUGIN_BASE . '/debug.txt', "call \r\n", FILE_APPEND);
        $sql = $wpdb->prepare("update {$table_slugs} set stat= stat+1 where slug_name=%s", $slug_name);
        $wpdb->query($sql);
    }

Your help is highly appreciated. Thanks

  • This means the page is running twice. Are you doing any redirection aka wp_redirect()? Check if anything is hooked with ulr_init_slug action. – Sisir Aug 21 '14 at 6:32
  • i am using wp_redirect() for redirect to the another url. – jogesh_pi Aug 21 '14 at 6:58
  • If the another url is within your WordPress site. The template_redirect will run again. So, the hook isn't running twice. For first one it runs for your current page and the second one from your redirected page. – Sisir Aug 21 '14 at 7:19
  • wp_redirect() i am using for third party link not for the same site, is it working same two time like you told? – jogesh_pi Aug 21 '14 at 7:22
  • hmm. Please add your redirection code with the question also how you hooking into it. – Sisir Aug 21 '14 at 7:32

Your Answer

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

Browse other questions tagged or ask your own question.