I need to create a plugin in wordpress that should be independent of theme. That means it can work on any theme. How can I achieve this?
As you say you need to create a plugin. Plugins are all independent from Themes.
Check out the official WordPress Codex entry for that.
Also if it is more simple just create a function in a separate file and include it via you functions.php with get_template_part(). Here are the Codex informations for that function.
All plugins are independent from any themes by default, so you don't need to worry about it.
If your concern is to avoid conflicts with all themes, there's no common rule to achieve that. It depends on what type of plugin you are creating and how are you creating it. You just have to think of all possible issues that may come later by any theme and make a way to avoid these.
Related
I am building a simple plugin of wordpress, and trying to use theme's function into plugin's file, but it is saying "Call to undefined function", while that function exists in themes.php of current active theme.that function is in use in theme's files but can't accessible in plugin's file, can anyone guide what to do?
while I have to attach plugin with theme as mandatory plugin.
Thank you
Plugins are loaded before the theme so the function hasn't been defined at the time you're calling it. Execute your code on a hook such as init instead.
E.g.
function wpse_my_plugin_init() {
myPreviouslyUndefinedThemeFunction();
}
add_action( 'init', 'wpse_my_plugin_init' );
See here for a list of hook options: https://codex.wordpress.org/Plugin_API/Action_Reference
The earliest opportunity you have to call this code would be after_setup_theme.
On another note I'd suggest rethinking this approach. A plugin should be an independent module; it shouldn't be tied to your theme.
Your approach/design is serious flawed. Plugin functionality should never rely on theme functionality. As already pointed out by #NathanDawson, plugins get loaded first, then child theme functions then parent theme functions, so your approach will never work as indented or without a lot of unnecessary bloated code
Plugins are meant to extend site functionality, and not theme functionality. I think this is where your idea is seriously flawed, and most probably due to a lack of knowledge on who should do what and when
As I have stated, plugins should give functionality to the site. Functionalities like favicons, custom post types, custom taxonomies, shortcodes, widgets, related posts and rewrite rules should be in a plugin. This gives functionality to your site, which simply mean, when you change the theme, these functionalities won't change or they won't be affected. Also, if the plugin is removed, it doesn't change the way how the theme looks and operate.
Theme specific functions are functions that gives functionality to the theme itself. Without these functions, the theme looses functionality or its looks. It simply means thant the theme changes. These functions includes functions to enqueue your stylesheets and scripts, theme suppport functions, sidebars, custom headers and backgrounds and registering image sizes.
If you are going to make a plugin rely on theme code, you should be looking to create a child theme, as child themes are meant to extend the main theme. Child themes inherit all functionalities from the parent theme, and can extend, modify or add new functionality to the parent theme.
To conclude, by making a plugin rely on theme code, you are making your site very vulnerable to breakage. If you change your theme, you will break your site as you will break your plugin. This is not how plugins and themes were made to interact. By creating a child theme instead, you void that risk as child themes cannot be activated without the parent theme being in the theme's folder and neither can any two themes be activated at the same time.
I have a wordpress website which includes a member area to allow each user to edit his profile.
For this member area, I found these bootstrap templates that would be perfect for what I need.
http://themeforest.net/item/caplet-admin-responsive-html-theme-/6537086
http://themeforest.net/item/webarch-responsive-admin-dashboard-template/6157416
So, I wanted to know if it's possible to convert or use in some way these templates on wordpress ?
Since I already use a wordpress theme, it would be easier to include the bootstrap template as a plugin, do you think it's possible ?
I also heard of child themes features on wordpress but don't know much about it, do you know if it could be a solution ?
Thanks !
You would have to use those templates and custom make your own theme, which is not an easy task. Themeforest has some nice themes for wordpress based on bootstrap. I would check those out instead.
I would like to know if there was any methodologies to inherit or extend a plugin files to theme directory as woocommerce did and customize them without touching the base/core files. please share any links or thoughts
Thanks
You're looking for WordPress's add_action() and do_action() functions. WooCommerce, for example, makes use of these functions by using hooks and filters for templates and functions.
For example, WooCommerce does things like do_action( 'woocommerce_before_main_content' );. This means that you can "add and action", override functionality, remove actions, and generally customize things how you see fit.
Not all plugins and themes ship with this functionality; but browsing the source code can often reveal instances of this. It's more of a courtesy by developers.
I understanding we may not have a plug-in that would fit in to our requirements in all respects. I am planning to use theme-my-login plug-in. However, it requires fair amount of customization in terms of adding more/custom fields for registering into site. There are some page redirections that I had to insert conditionally.
Can I tweak the code with-in plug-in files?
Should I be extending this through themes files/folders, withough tweaking in the plug-in file structure?
Thanks in advance
It all depends on what you want to change / add, and what the target plugin allows you to do. In general, you can customise some of what other plugins are doing by making use of available filters, actions, CSS, templates, and brute-force WordPress filter/action hooks if that's appropriate.
The Theme My Login, which you reference, presents its various views through templates that you can override either by dropping a customised copy of its template into your theme, or through a filter "tml_template". If what you want to change is the appearance, then you have lots of control through those two avenues. I notice that the plugin has various other filters you can hook, allowing you to customise many aspects of the plugin.
I suggest you start off by reading the plugin's FAQ which points you in the right directions already.
You can tweak the code with anything in Wordpress, but with caution.
If you are customizing wordpress themes, when the themes are updated by wordpress, any customizations are lost.
That is why Child themes are created.
You may find this link useful:
http://codex.wordpress.org/Child_Themes
I suggest creating a new plug in based on the customized code of the Wordpress plug in.
You may find this link helpful. It discusses Wordpress plugins and how to create them.
https://codex.wordpress.org/Writing_a_Plugin
I am trying to see if I could change a plugin's admin options within my theme functions?
For example, the plugin allows you to upload a logo. Can I filter it somehow to remove that option from the plugin within my theme function.php?
Thanks...
It depends on if the plugin is hookable. Most of the time the answer to that question is no, unfortunately. I would approach the plugin author regarding that.