Where to put add_action() in WordPress - wordpress

I am new to WordPress. I was referring to code of WordPress theme. I am confused about where to put our function and add_action() for that function.
I want to know whether it is right to define function and add_action in one PHP file and put it anywhere in our theme folder.

"functions.php" is the file where you want to put your custom functions, hooks and actions. If your theme doesn't have a functions.php just create it. Wordpress will get the contents of it automatically, so you will have access to defined functions anywhere in your theme.

Related

How is functions.php loaded in Wordpress?

I am starting to study Wordpress a bit deeper and i d like to know which way functions.php file is loaded so that every function is available everywhere in the theme files.
Or at least if you suggest me some source where to find this information.
Thank you
A function.php file:
Executes only when in the currently activated theme's directory.
Applies only to that theme. If the Theme is changed, the functionality is unused.
Requires no unique Header text.
Is stored with each Theme in the Theme's subdirectory in wp-content/themes.
Each theme has its own functions file, but only the functions.php in the active Theme affects how your site publicly displays. If your theme already has a functions file, you can add code to it. If not, you can create a plain-text file named functions.php to add to your theme's directory.
Source # https://codex.wordpress.org/Functions_File_Explained.
In short, the function.php file is a function wrapper. Each function is hooked to a specific action hook. These actions are called when a user opens a page (Server request).
You can get a better understanding of how action hook work by looking at the Plugin API/Action Reference which represent a typical hook firing sequence.

Creating a wordpress theme

Just want to know that if I'm going to create a new wordpress theme, what about functions.php, should I need to create a new functions file, or just copy it from other wordpress theme. I have read codex but it only tells about the templates needed to create a new theme.
The theme functions file is a template used by Word-press themes. It acts like a plug-in and gets automatically loaded in both admin and front-end pages of a Word-press site.
The functions.php file can be found in your theme’s folder.
You don't need to create a new functions.php file when you want to put your own function in function.php just paste that code in your themes functions.php file.
You can look into these Reference Links to understand more about function.php file :-
Functions_File_Explained
You don't need a new functions.php file if you don't want custom functions in your theme.
See this thread for more information - not including functions.php doesn't make any difference for your custom theme.

(WP) Override Plugin Function In Theme Functions

How to override function inside plugin and put in theme functions.php file? I've try using remove_filter and add_filter, apply_filter, add_action etc, but still not worked.
https://github.com/mikejolley/WP-Job-Manager/blob/master/wp-job-manager-template.php#L72
I want to override function on line 72. How can I done this? I don't want to edit plugin files because I just want this function replaced only when this theme activated. If not activated, function will be as the original function.
This function used by plugin. And no any filters or actions inside. So, my sorry, you can't solve this issue.

Can my WordPress custom templates be in the plugin folder or only in the theme folder?

A WordPress theme I am developing has an integrated custom post type called "albums" which utilizes a few custom templates (archive-albums.php, content-albums.php, etc.). What I want to do is transfer this functionality, along with the template files, into a plugin for the sake of portability.
I transferred the CPT code from the functions.php with success, but when I try to move the template files from the theme folder to the plugin folder, things fall apart. I feel like it should be simple to somehow register the templates so WordPress knows to load them.
Can my WordPress custom templates be in plugin folder or only theme folder?
Things are falling apart because when you move those files, you're violating WP's native template hierarchy. You'll need to explicitly declare the location of those files. Using the archive as an example, you could add something like this to functions.php (to tell WP to look elsewhere):
add_filter('template_include', 'include_album_template', 1);
function include_album_template($template_path) {
if(get_post_type() == 'albums') {
if(!is_single()) {
$theme_file = 'path-to-your-plugin-directory';
$template_path = $theme_file;
}
}
return $template_path;
}
Obviously you'd use your own path, and I wrote this hastily so you might want to refactor.
I have the same issue. I'm already using add_filter ('template_include', ...) problem is that I need to specify a file to return, and in this case being it,index.php. This raises an issue with the theme not running entirely as if installed via themes folder, because what I need is to have WP selecting the appropriate file to render without any conditional logic from my part. So if it is a post it will select the single.php and so on. Another problem raised with this method is that in header.php the call get_header (); ignores the local file header.php and loads the default theme installed file instead.

wordpress jquery inclusion by default

I'm using wordpress 3.3.1 and it seems that jquery is there by default, all i need to do is to enqueue the file through wp_enqueue_script function. What if i want to put this function inside another plugin that is already installed. For example, i'm using jwplayer plugin for wordpress now if i want the jwplayer plugin to enqueue the jquery file for me where do i tell it to do this, the main plugin file for jwplayer is named as jwplayermodule.php and it's at the root of the jwplayer plugin directory.
Regards
You enqueue jquery like so:
// Function to perform any things that need to be done BEFORE the header is called
function my_wp_init() {
wp_enqueue_script('jquery');
}
// Enable the widgets, prepare the function for the head call, etc.
add_action('init', 'my_wp_init');
The best place to do this would be in your theme's functions.php file. This way, jQuery is always loaded in your theme.
ALTERNATIVELY, you can drop the same code into the plugin php file you mentioned - jwplayermodule.php

Resources