Joy
(@joyously)
“when I need it” is the key phrase here.
WP already handles the database connection for you, so you don’t need to write any functions for that.
Custom code should always go in a plugin, unless it’s modifying the theme in which case it should be a child theme.
But “when you need it” is really handled by attaching your code to actions that fire at the appropriate context.
https://developer.wordpress.org/reference/functions/add_action/
If you are wanting to affect the content on different pages, it could be a shortcode
https://developer.wordpress.org/reference/functions/add_shortcode/
or a filter
https://developer.wordpress.org/reference/functions/add_filter/
or if it’s the whole page, it could be a page template in a child theme.
So, here is the actual situation.
I am modifying pages for a church site.
Currently, there are queries against recorded teachings (search by Title or by key word in the description or by Scripture reference.
I am modifying the searches to also search written articles on the site, stored in the database.
Here is what I wanted to do – and you can tell me “NO. Don’t do that!”.
I want to take the function I have written on a page and take it out of the page and store it somewhere that I can call it. I would like to call it on the Teaching Search pages as well as call it from pages that only search the articles.
What is the best method to achieve this?
dpimental
I want to take the function I have written on a page and take it out of the page
Can you expand what you mean a (PHP) function you have written on a page? This sounds strange language. Where exactly have you written this – in a page template file for instance?
We have a plugin that allows us to enter php code on a wordpress page.
So, I have some code that takes the input of the text box and uses it for the criteria of an sql query.
So, I have this code on a page and I want to be able to reuse it, without having the same code on multiple pages.
I was thinking if there was a way to store the code in one place and then simply call it on the pages it was needed that would be helpful.
Is there a way to do that?
dpimental
The simplest way is to create a shortcode and then you can add the shortcode to any page you like.
https://generatewp.com/shortcodes/
Many tutorials suggest putting such snippets in functions.php but I advocate that you should create a plugin, plugins are really easy at a basic level, just a php file with a header
See this https://gist.github.com/alanef/87d367877dd9ad228606ecc0e9fc732a/ for an example
Much thanks for your assistance.
I will try creating the plugin.
dpimental
I just want to make sure I am following best practices. I found an example and want to make sure it’s correct.
My goal is to create a plugin and pass a search term to it.
The plugin would then connect to the database, search the database using the search term as criteria and return a formatted result set.
So, in the plugin, I was going to do the following.
1. do_action( ‘my_plugin_hook’ );
2. add_action( ‘my_plugin_hook’, ‘my_plugin_echo’ );
3. Create Function
function my_plugin_echo( $url ){
echo “The home url is $url”;
}
On my page, I was going to do the following.
1. [my_plugin_hook]
Am I doing this correctly?
Can I use the “hook” on any page I want?
dpimental
You are doing it wrong. Please read the add shortcode link I gave you.
So, I use this code:
// Add Shortcode
function custom_shortcode() {
}
add_shortcode( '', 'custom_shortcode' );
And the empty string ” I put in what I want the shortcode to be named.
And the ‘custom_shortcode’ is the function being connected to the shortcode?
dpimental