Community Forums › Forums › Archived Forums › Design Tips and Tricks › How do I output the returned value of a function that uses an earlier hook?
Tagged: action hooks, genesis_init, genesis_meta
- This topic has 7 replies, 2 voices, and was last updated 8 years, 10 months ago by simbasounds.
-
AuthorPosts
-
March 13, 2016 at 1:11 am #181299simbasoundsMember
I'm using theme-specific hooks in my question, but I would think the question would apply to action hooks in general. Here's an example of my function:
add_action( 'genesis_meta', 'page_source' ); function page_source() { if (dynamik_has_label('test')) $page_source = "test"; return $page_source; }
Note: genesis_meta or genesis_init is required for retrieving the value of dynamik_has_label()
Using the function
This does work:
add_action( 'wp_head', 'echo_source' ); function echo_source() { echo page_source(); }
This does not work:
add_action( 'genesis_footer', 'echo_source' ); function echo_source() { echo page_source(); }
I can understand why it possibly wouldn't work. It's asking my page_source() function to be called after the genesis_init hook has been already been executed (although I'd like to understand why it works inside wp_head).
My general aim is to find a solution that reduces the use of globalised variables, and write reusable functions with return statements. To what extent is this possible in this scenario?
Note: I asked this question on stackexchange.wordpress. It's Genesis-specific as well as being relevant to WordPress action hooks in general.
http://wordpress.stackexchange.com/questions/220520/how-do-i-output-the-returned-value-of-a-function-that-uses-an-earlier-hookMarch 13, 2016 at 5:13 am #181317Victor FontModeratorIt may have something to do with the dynamik_has_label function. Look at the function's source:
function dynamik_has_label( $label = 'label' ) { $labels_meta_array = dynamik_get_custom_field( '_dyn_labels', false, true ) != '' ? dynamik_get_custom_field( '_dyn_labels', false, true ) : array(); if( is_singular() ) { if( in_array( $label, $labels_meta_array ) ) return true; } return false; }
It's possible that the function is returning false in which case you would not have anything to echo. You are setting the $page_source to test only if the dynamik_has_label returns true. Your code may be working but you are not accounting for the all of the conditions that need to be addressed.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 13, 2016 at 9:38 am #181345simbasoundsMemberHi Victor,
Thanks for responding. I can confirm the dynamik_has_label function is returning true, or at least in the case of:
add_action( 'wp_head', 'echo_source' ); function echo_source() { echo page_source(); }
ie. it works on the page where the "test" label is active, hooked into wp_head, but not genesis_footer.
I probably made my question a little too open to ambiguity by phrasing it in the context of the Dynamik Website Builder. Ideally, and especially in the case of Stack Exchange I should probably have limited it to WordPress functions.
I'm going to wait and see what CobaltApps say about this specific case, then reply with their answer.
Then time-permitting, see if I can create a more generic example to answer my original question.March 13, 2016 at 10:39 am #181349Victor FontModeratorTesting for true in wp_head does no good if the problem is in the footer.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 13, 2016 at 10:44 am #181351simbasoundsMember..hence my question: How do I output the returned value of a function that uses an earlier hook?
March 13, 2016 at 6:26 pm #181369Victor FontModeratorYou can run php functions in multiple hooks. You are not limited to a single hook for any function.
The dynamik function is reading a custom field and returning true when a single page, post, or attachment has that custom field assigned to it. When you use wp_head with your example above, you are writing the word "test" somewhere within the head tags. Nothing within the head tags display on the page. You should also wrap the content in meta tags or something that won't cause a HTML validation error, otherwise you will not be able to reuse its content anywhere else on the page.
I don't know what you're trying to accomplish, but you can most definitely output content in the footer. You can most definitely display the value of a custom field in the footer. And you can most definitely read HTML elements within the head tags if you use jQuery. Maybe if you can be more specific with your requirements, we can provide a definitive answer.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?March 14, 2016 at 12:39 am #181381simbasoundsMemberre. "Nothing within the head tags display on the page. You should also wrap the content in meta tags or something that won't cause a HTML validation error, otherwise you will not be able to reuse its content anywhere else on the page."
Actually it does display when used in wp_head, but in any case it's just a temporary test so validation doesn't matter at this stage. I'm simply using those hooks as a sort of console.log. I inspect the page source to see if the hook output or not.re. "Maybe if you can be more specific with your requirements, we can provide a definitive answer."
As I said, I will create a more generic example for this forum - which I value highly - to answer my original question. Thanks for your eagerness to help, Victor. I really appreciate it. I'll do some testing in my own time and be back with a better question, perhaps an answer too.March 19, 2016 at 6:25 am #181842simbasoundsMemberJust to wrap this up: I solved my specific issue by using globalised variables to pass values between action hook functions.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.