WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] actions not being removed when told too... (11 posts)

  1. Adam
    Member
    Posted 2 years ago #

    I am trying to use hooks to display default content for a new theme install thus I have something like:

    function themename_404_err_hook(){
        do_action('themename_404_err_hook');
    }

    then some where I have: (on the 404 page)

    function themename_default_404_err_message(){
         _e('What your looking for does not exist');
    }
    
    add_action('themename_404_err_hook','themename_default_404_err_message');

    when themename_default_404_err_message is called it properly out puts: What your looking for does not exist.

    The problem is that when, in my functions.php, I do something like:

    function themename_my_new_404_err_message(){
        _e('you made a booboo');
    }
    add_action('themename_404_err_hook','themename_my_new_404_err_message');
    remove_action('themename_404_err_hook', themename_default_404_err_message);

    I get: you made a boobooWhat your looking for does not exist

    when I should get: you made a booboo

    am I not understanding hooks properly?

  2. Josh23french
    Member
    Posted 2 years ago #

    You're missing quotes around the second argument to remove_action... that would explain the default message not being removed.

  3. Adam
    Member
    Posted 2 years ago #

    oops I should have used an actual example to avoid this:

    this is my hook:

    function aisis_404_err_message_banner(){
         do_action('aisis_404_err_message_banner');
    }

    Then some where I do:

    function set_aisis_404_default_err_message(){
         _e('cannot find what you want');
    }
    add_action('aisis_404_err_message_banner', 'set_aisis_404_default_err_message' );

    So now we have added the action so when ever we call: aisis_404_err_message_banner we get: cannot find what you want

    perfect. But then in my functions.php I did:

    function set_new_aisis_404_err_message(){
        _e('booboo occured');
    }
    
    add_action('aisis_404_err_message_banner','set_new_aisis_404_err_message');
    remove_action('aisis_404_err_message_banner', 'set_aisis_404_default_err_message' );

    What I get now?: booboo occuredcannot find what you want

    What explains this?

    All the quotes are there, as far as I know I am doing this right.

    also changing the order of remove and add does not change the outcome.

    even just removing the action does not do a thing, when the aisis_404_err_message_banner is called I still get the default out put of: cannot find what you want

  4. Josh23french
    Member
    Posted 2 years ago #

    Where exactly do you add the default message? It seems as if you add it after your call to remove it.

  5. Adam
    Member
    Posted 2 years ago #

    nope. I am calling it before that. I have a 404.php and when ever the user searches for something that doest exist that page is thrown this function is defined and added to the action in there, the action is then called where I want the message displayed.

    In my understanding I am calling it before i call the remove action. As functions.php comes AFTER the theme its self loads - no? should I not be defining the function that gets added to the hook in 404.php? should i be defining it else where and then adding it to the hook and then call the hook in 404.php?

  6. Josh23french
    Member
    Posted 2 years ago #

    functions.php loads before anything.

  7. Adam
    Member
    Posted 2 years ago #

    that cant be right - then my action would be removed and I wouldnt see anything. (its probably right im just doing something wrong). So if thats the case, I define the function to display the message in 404.php and add it to the action in that file and print out the action thus giving me my defult output. I then in functions.php remove the action and reload the page - the default out put still shows. why is it not being removed?

  8. Josh23french
    Member
    Posted 2 years ago #

    functions.php gets called: it adds the custom action and removes nothing.
    404.php gets called: it adds the default action and does the action

    The custom text is shown, then the default, right?

    You should put the default action in functions.php and the custom one in 404.php.

    So:

    # functions.php
    
    function set_aisis_404_default_err_message(){
         _e('cannot find what you want');
    }
    add_action('aisis_404_err_message_banner', 'set_aisis_404_default_err_message' );
    # 404.php
    
    function set_new_aisis_404_err_message(){
        _e('booboo occured');
    }
    
    add_action('aisis_404_err_message_banner','set_new_aisis_404_err_message');
    remove_action('aisis_404_err_message_banner', 'set_aisis_404_default_err_message' );

    After that point in 404.php, whenever you call do_action('aisis_404_err_message_banner'); you'll get what you want.

  9. Adam
    Member
    Posted 2 years ago #

    what if a user comes a long with this theme right and sais, I want to change the hook now. So with custom in 404.php and the default in functions.php would the user be forced to edit the "core files" of the theme? - in this cases 404.php -

    would a custom-functions.php loaded into functions.php at that point work if you dont really want your users altering the theme files?

    this gets into theme frameworks....but still applies.

  10. Josh23french
    Member
    Posted 2 years ago #

    If you don't want users editing "core files", don't put things in them they want to change. Make an options page: use the Settings API

    If you must have users editing files, anything would work as long as it's in the proper order... if custom-functions.php is loaded after or from functions.php, it'll work.

    Does switching the placement of the functions work as expected?

  11. Adam
    Member
    Posted 2 years ago #

    I figured it out then refreshed the page and saw this LOL. but thanks ill check out your link

    What I have is a file where all the hooks are defined, like:

    function test(){
    do_action('test');
    }

    then I set up a test action in that file, and printed it out on the 404.php page and that worked, then i removed it in the functions.php and it was removed. so it works :D thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic