• Resolved jgstroup

    (@jgstroup)


    Reading up on how to use the “post format” function I found a reference on the wpexplorer.com website:

    https://www.wpexplorer.com/wordpress-post-formats-guide/

    This guide describes the function that must be added to functions.php, and follows with an additional function call that must be added “add_action”.

    Looking though the code in my functions.php file I notice that every instance of the “add_action” function call is within the a function definition.

    So my question is: Is this an error in the example offered by the wpexplorer.com?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Looking though the code in my functions.php file I notice that every instance of the “add_action” function call is within the a function definition.

    It’s a little unclear what you meant with this.
    It wouldn’t work if all the add_action calls were in a simple function, but they could be inside a class function.

    What you need to know is that WordPress loads part of core, then the plugins, then the theme, and then continues with the rest of core to handle the request. When a PHP file is loaded, whatever is not inside a function is executed. So you want the add_action calls to be run during the initial load of the file.

    Thread Starter jgstroup

    (@jgstroup)

    Thank you Joy –

    To clarify – the example of added code to functions.php that exists in the link I provided, has the “add_action” call after the new code for defining the function.

    As you say – anything outside the function definition will be called when the code in that file is called.

    Every other example of functions I found had the “add_action” call before the conclusion of the function definition.

    So I was just wondering if the code in the example I cited is flawed and had a bug by placing the “add_action” call outside the function – of if it’s correct in this instant. (and if so – why?)

    The link to the example I cited is: https://www.wpexplorer.com/wordpress-post-formats-guide/

    Thank you!

    Moderator bcworkz

    (@bcworkz)

    The only code I see on that page has the add_action() call outside of wpexplorer_add_post_formats() function.

    In any case, if the function containing an add_action() call executes before the action fires, it’ll all still work. You need to think about order of execution (not an easy task in WP) more than whether it’s in a function or not.

    One reason to place an add_action() call inside a function is if there is some condition where we don’t want to always add an action, and that condition isn’t yet available when plugins are loaded.

    Thread Starter jgstroup

    (@jgstroup)

    bcworkz –

    You are so right on all counts.

    My question is why the code in my specific example might have the “add_action” call outside the function, when every example I’ve found on my installation has this call inside a function.

    My guess is that this is a bug in the example – but I’m not experienced enough yet to know for sure – and I thought someone might know off the top of their head.

    Thanks!

    when every example I’ve found on my installation has this call inside a function.

    I don’t know where you’ve been looking, but that is not typical.
    It is typical to put the add_action() call outside of any function so that it is executed when the file is loaded.

    Thread Starter jgstroup

    (@jgstroup)

    Thank you Joy for taking such a close look at this –

    The example cited early in this thread is:

    https://www.wpexplorer.com/wordpress-post-formats-guide/

    You’ll find in the code example about half-way down the length of that page an example that has the “add_action” call after the function definition.

    My suspicion is that the code example is in error – but I’m not sure. Hence, my question about it here.

    Many thanks!

    Yes, you’ve said that several times. I’m saying it’s not, and it would be typical for add_action to be outside all functions.

    Thread Starter jgstroup

    (@jgstroup)

    Joy –

    I’m just getting a handle on what’s “typical.” I appreciate your help.

    When I do a search on the wp-includes/functions.php for “add_action” there are three cases – all are “inside” a function. Typically, right before the closing curly brackets.

    I just started building a child theme – and so far I’ve used “add_action” once – and it is outside a function.

    So I’m learning that it can go either way – depending on the case.

    Again – many thanks!

    In this case, you can’t compare what core does typically to what a theme or plugin does, because the actions belong to core and whatever you are writing isn’t core.
    You would have to compare to other plugins or themes to find what is typical.

    Thread Starter jgstroup

    (@jgstroup)

    Thank you Joy

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘where to place “add_action” function call’ is closed to new replies.