Replies: 0
I would like to know how/where to include my PHP code, within WordPress, so that it is not processed too early.
I find it hard to write the question properly because I don’t know enough to know what to ask!
So I have detailed the setup I have right now, and what is wrong with it – so then maybe someone can understand what I need to do and enlighten me.
Thanks in advance for any help / suggestions.
Current setup:
I have defined a function, my_function() in a new php file, my_file.
This file is located in the wp-content folder.
To register this folder, I added the following line in functions.php:
include(WP_CONTENT_DIR.'/my_file.php');
The my_function() runs a MySQL query, and outputs the result.
I added a shortcode for this function in functions.php:
add_shortcode('my_function_sc', 'my_function');
When I add this short code to a page, and navigate to the page, all is well – the query results are displayed.
However, I notice that this short code seems to be processed before any other content that I add to the page.
For example, if I type the following in to the text field for a new page:
Hello 1 2 3
[my_function_sc]
Goodbye 4 5 6
And then open the page in a browser, I see the following
Results from query
Hello 1 2 3
Goodbye 4 5 6
Question:
What is the preferred way to organise my PHP code so that I can call a shortcode ‘inline’, from the text field of a page?
Using the above example, I want the output to be:
Hello 1 2 3
Results from query
Goodbye 4 5 6
If you just tell me in a few sentences what the best way to do this is, I can then research it etc.
Because I am new to WordPress. I find it difficult knowing what terms to search for etc.
I have read a little about ‘hooks’ and it seems that I need to hook the code to the right part of WordPress so that it is run while the content of the text field is being processed.
Thanks
Garrett