add_filter hook not working in plugin's php file - wordpress

I am developing a plugin and it works fine when I call my code from my plugin's main php file but I want to run only once My external file is called in the plugin. I mean I want to place my function inside includes folder and in a file living there. like plugin folder/includes/file.php ... here is my code
add_filter('wp_nav_menu_items',',my_custom_function');
function my_custom_function ($nav){
return $nav."<li class='menu-header-search'><a href='#'>Icon</a></li>";
}
Kindly let me know how can I make it work, it works in side plugin's main php file but does not work from other php files inside plugin includes folder.

It should work if you include your file from the plugins main file, just make sure it is included correctly.
include plugin_dir_path( __FILE__ ) . 'includes/file.php;
This would include a file in an includes folder in your custom plugin folder, and would include an file named: "file.php". If you want to know if its been loaded correctly, just add an die() statement in the included file:
die('my include file has been loaded correctly');
Just remove the die() statement after you have an confirmation that its working :) Lastly you need to copy your callback function code and paste it in your included file. If your hook still does not work, then it might be something wrong with your callback function.
The documentation for the filter hook you are using:
https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/

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.

How to add a file to wordpress theme from apparence->editor?

How to create a new file in my wordpress theme without using ftp client ?
thanks !
Using touch(); PHP function, which creates a new file in the specified directory. This is how I suggest doing it (and admittedly, there are cleaner ways, but this will get the job done, quickly and effectively)…
Open header.php — then write this code in the very top of the file:
<?php touch('wp-content/themes/YOUR_THEME_DIR/FILE_NAME.php');?>
Replace YOUR_THEME_DIR with the directory in which your WordPress theme lives.
Then, replace FILE_NAME with the name of the file you want to create.
Once that’s all done, save Header.php and go to the homepage of your site.
As soon as the homepage loads, it will create that new template or theme file.
Source :
https://www.webmechanix.com/how-to-create-a-new-theme-file-in-wordpress-without-ftp-access/
You can use a plugin called WPIDE found here https://wordpress.org/plugins/wpide/
This plugin will allow you to edit and add folder and files in your project.

Changed template name and now wp_list_comments() is not working

I had to change the template folder name because it had a space it in. That messed with my HTML validity.
So 'Panda Productions' -->became--> 'PandaProductions'
Now wp_list_comments(); doesn't work. Comments aren't showing up.
When I changed the name back, it worked again.
Basically, in single.php (or in other pages optionally) WordPress adds comment template depending on function call of comments_template(), which means the comment template is a separate file and by calling this function (mentioned above) we add comment template in files/pages like single.php, this functions accepts optional parameters and one parameter is being used as a filename to load the template file, if a filename is supplied with the function then it loads that file as the comment template, otherwise it looks for /comments.php file in the same folder by default. You can see the source of this function here.
So, I think, the problem is that, this function (comments_template()) is called in your single.php with the folder path/name and that's why it's not being loaded after you change the folder name. If you search for this function call then you may find the path and filename that used in your theme and you can modify those as well.

hook_preprocess_page() does not seem to use the suggested template file

I am suggesting a template file in the hook_preprocess_page() implementation done from a module, but the suggested template file doesn't seem to be used.
The template file is page--terminal-template.tpl.php, which is in the directory containing the module, and this is the implementation of hook_preprocess_page().
function terminal_preprocess_page(&$variables) {
if (arg(0) == "terminal") {
$variables['theme_hook_suggestions'][] = "page__terminal_template";
}
}
Could anyone please help me?
Preprocess and process functions can be implemented by modules. In fact, the documentation for theme() lists them when it shows in which order those functions are called.
The fact is that Drupal looks for the suggested template files in the theme directory. You have these alternatives:
Put the template files your module is suggesting in the directory containing the theme currently used
Follow what reported in Load view template on module activation to load the template files from the module directory
Suggest the template files you want to use in a preprocess function implemented by a theme
Following what reported in the other question, you would be able to use the template file found in the module directory. The only problem is that you would be using a generic template that could be different from the default page template used from the currently enabled theme.
If you are adding template files for the currently enabled theme, you should call drupal_theme_rebuild() to make Drupal rescan the directory containing the template files, after you added the new template file to the theme.
Actually, this hook can also be called from theme's template.php file along with module's hook.
Please refer Drupal 7 documentation here.
Say if your active theme is MY_THEME, then the code should be:
function MY_THEME_preprocess_page(&$variables) {
if (arg(0) == "terminal") {
$variables['theme_hook_suggestions'][] = "page__terminal_template";
}
}
And the template suggestions will work.
Edit: This functionality can also be implemented with Modules using hooks.

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.

Resources