Plugin disappears on frontend after theme changing - php

I am developing a plugin for Woocommerce. I thought it would be nice if the plugin looked nice in different themes. So I downloaded a theme and installed and activated it.
So I checked the plugin on the product page. But it wasn't there!
I changed back to some other theme and it showed up again!
So my question is, is this my fault for developing a not-so dynamic plugin?
Or is this the fault of the theme creator?
And what are the possible explanations/solutions for those kind of problems?
Example:
Twenty fifteen:
Other theme:
The elements of my plugin are not there as far as I've seen.
Edit
I am using the woocommerce_after_main_content hook.
Which I do like this:
add_action( 'woocommerce_after_main_content', 'ws_action_woocommerce_after_main_content', 10);
This calls my function which runs a shortcode.
Like this:
function ws_action_woocommerce_after_main_content() {
do_shortcode('[ws_frame]');
}
This shortcode ofcourse has the function with <h1>Hello, I'm here!</h1>
Summary of how my plugin works
My plugin is a designer plugin for shirts (May aswell be for other products).
The plugin has an options page in the backend.
My plugin only gets loaded if Woocommerce is active. Also, the plugin only gets shown on the product page IF the product is in the specified category.
Using shortcodes, the front end layout is generated in a function, where some enqueued scripts and css is present. The function also has some HTML.
As mentioned above, I am using woocommerce_before_main_content hook. This calls a function which has the do_shortcode('[ws_frame]') in it. This shortcode as the enqueued scripts, css and has some HTML. Yet, I do have ob_flush(); before the do_shortcode('[ws_frame]'). So this might be something?
Also, there isn't much more going on for the front end of this plugin. This is what have done to make it appear on the product page. The options page in the admin panel still works.
The question still remains, is it me, who needs to fix this, or is this something the theme creator is responsible for?
And how can I fix this myself? How can I make sure all of the hooks I'm using are still available in that theme? Even if it is the theme's creators fault, I would like to know how I can fix this myself.

I may be wrong but I guess in your case the theme for which your plugin is not displayed has the particular action removed. (you may check that in the respective theme's functions file.) Well, it is better idea to hook your plugin (functionality related into some action hook which is related to functionality or which renders some funcitonality (rather than being an UI related action).
Like, the action you have used to hook your plugin woocommerce_after_main_content along with another action, only outputs an html element wrapper. (which some themes might remove and use their custom html wrapper.
In your case since your plugin is related to / displayed on single product page, I would recommend to use relevant action to hook your plugin. e.g. woocommerce_after_single_product_summary or woocommerce_after_single_product, which are generally not removed by woocommerce compatible themes.
Hope this helps.

Related

Where to add a filter in functions.php of my Wordpress theme?

On wordpress 4.9.1, the plugin Menu Social Icons is installed and the theme Twenty Sixteen is being used. The plugin works as expected, except for Stackoverflow and Vimeo icons. The problem is that for these two websites a generic icon will be shown, instead of the one corresponding to these sites.
In the documentation of the plugin it can be found that this issue is well known and they present the solution. However, due to my lack of php knowledge, I have not been able to make it work.
Their solution:
Option: Add Vimeo and Stack Exchange
To use FontAwesome 4.0+, which drops support for IE7, but adds
vimeo.com and stackexchange.com, add this to your theme’s
functions.php file: add_filter( ‘storm_social_icons_use_latest’,
‘__return_true’ );
I have tried including the line add_filter( ‘storm_social_icons_use_latest’, ‘__return_true’ ); in different locations of the functions.php of my theme, without positive results.
Where would be the correct location to add this line of code in the php file?
In terms of their solution not working, I'd suggest that the issue probably isn't in your functions.php file, as all filter functions in your current functions.php file should work.
A couple of notes to consider though:
If you need to modify a WordPress theme, your best option is to create a "Child Theme" to make the changes in, otherwise your updates will be overwritten whenever your theme is updated.
Also, best practice when working on WordPress themes (or just PHP in general) is usually to add your changes at the bottom of the file you're modifying, after a comment to say what they are, and before the ?>.
A good example in this case would be to modify your functions.php file so it finishes like so:
//function to include latest FontAwesome changes - 31/12/2017
add_filter( 'storm_social_icons_use_latest', '__return_true' );
?>
Although, I can't stress to you how useful child themes are in situations like this.

How do I unhook an action? My current method does not seem to be working

I am working on a WordPress website, with WooCommerce functionality.
I am currently trying to remove the social sharing feature, which is displayed on the Product Pages. I am trying to achieve this through the removal of the Action from its Hook, by placing the following code in the functions.php file:
<?php
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
?>
Said functions.php is placed within the Child Theme I have created.
Unfortunately, this is not removing the social sharing feature. Does anyone know why? To see if there was an issue with the Hooks, I added an Action to the same Hook, to which this worked.
Could it be possible that the Social Sharing feature has been placed in some parent folder which overrides/has higher priority over the functions.php file in the Child theme? If this is the case, what would be my options in getting said feature removed. I can only think of placing the affected file in the Child theme and then manually removing the relevant coding. This is a last resort as it may cause problems, at a later date, when parent files are updated etc.
woocommerce_template_single_sharing() calls the Woocommerce template located at woocommerce/templates/single-product/share.php where there's a do_action call for other plugins & themes to add social sharing. If you want to "block" plugins from using that, your best bet (if you're building a theme) would be to use the built-in Woocommerce template override feature and simply prevent this do_action from firing. This would not cause any problems unless Woocommerce did a massive, structural rework. But otherwise, updating Woocommerce and any other plugins or a parent theme wouldn't affect your child theme.
In your child theme, create a directory called woocommerce, add a sub-directory called single-product, and then copy the share.php file from the above location in the plugin to that location in your theme.
Comment out the do_action and voila! No more social sharing and anything that hooks into that do_action won't run.
As an alternative...if you know exactly what's hooking into that do_action, you would run a remove_action on that bad boy, but then you need to hunt these things down and manually update your call any time a plugin tries to hook into it.
You could use https://codex.wordpress.org/Function_Reference/remove_all_actions
add_action('plugins_loaded', function(){
remove_all_actions('woocommerce_share_hook');
});

Wordpress & Genesis Framework linking

I created a Page in wordpress called add-a-contact. Contains name, email, and phone number.
I created a custom php page called contact-insert.php which will accept the form fields and insert into a database.
How do I link the Page add-a-contact so when the submit button is clicked it posts to contact-insert.php
I was asked to take over from a previous developer and haven't figured out how Genesis and Word Press are linked together. I was told I can't put the source in functions.php.
Was wondering if anyone could shed some light on this?
Jim
If I understand correctly. You want to have your add-a-contact WordPress Page, have its own php code applied, in WordPress we call it custom page template.
You said, you created a page called contact-insert.php. I would suggest to turn it into a simple page template in your Genesis child theme.
For sample code of how a custom page template looks like in genesis child theme, have a look in this tutorial http://www.wpmayor.com/creating-custom-template-in-genesis-framework/
To create custom template in Genesis you only need few things to do it right.
Put all your code in a function, and then hook that function to some hook, in our case, I suggest do it to genesis_loop
May remove the default loop from that page
Last line of the php file should have genesis(); so it inherit everything from Genesis to become part of Genesis theme.
e.g.
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_do_custom_loop' );
function child_do_custom_loop() {
//your custom php code here
}
Then apply this template your add-a-contact page from custom page template setting https://en.support.wordpress.com/pages/page-attributes/#template
While I would definitely recommend Mohsin's approach, a simpler answer would be to have your contact form's action attribute point to the URL of your custom .php page, so that on submission, custom page can read values from the $_POST superglobal (or via filter_input() if you're feeling fancy).

How to place plugin hook in a wordpress template

In many WordPress plugins, there are instructions that Place <?php do_action('plugin_name_hook'); ?> in your templates.
where exactly this code needs to be placed and what should be the plugin_name_hook?
hooks are actions performed at a time.
If you want a custom hook then
1.create your custom hook in your plugin or theme's function.php
add_action('my_action','my_function');
function my_function()
{
// do something
}
2.and call it in your template as
do_action('my_action');
and you can read more here
https://codex.wordpress.org/Function_Reference/do_action
I don't really like plugins that you still need to modify your theme's template file manually. What is the use then of a plugin.
To come back to you question though, your do_action() call will need to go in the template where you need to display the output of the plugin. Say it is a plugin that add social share buttons to the content, you will need to add the do_action after the_content() in your template files.
The second part of the question, the plugin_name_hook will be specified by the specific plugin. So far that you have to follow the installation instructions of the plugin

How to add a widget by its shortcode on Wordpress posts / pages?

Is there any way we can assign a shortcode to already created widget and later use that
shortcode in our specific posts and pages to show that widget instead of trivial method of
showing widgets in sidebar ? I googled about this stuff didn't find anything relative.
Any suggestion would be welcome!
Yes, You can call widget in Post/Page using shortcode. Follow this link to establish a system like that.
http://digwp.com/2010/04/call-widget-with-shortcode/
This might be relevant.
Widgets on Pages
https://wordpress.org/plugins/widgets-on-pages/
Observations and two questions:
This plugin allows insertion of 1 or "sets" of widgets via shortcode into pages, posts, (and Custom post types?) and it will also save a list of configured shortcodes~sets. VERY handy!
When comparing its features to the plugin Widgets Shortcode, that plugin does not provide a means to save "sets" of plugins, rather it seems to focus on inserting widgets individually. In fact, it inserts the shortcode for each widget in the bottom edge of each widget and it assigns different shortcodes to each instance of a widget. Also nice.
https://wordpress.org/plugins/widget-shortcode/
I like the granularity Widgets Shortcode provides with an individual shortcode for every instance of every widget. When running it and your plugin simultaneously even widgets saved in your plugin's "sets" are still assigned individual shortcodes. Those individual shortcodes allow using a specific widget out of a "set" in a different location without needing to create an additional instance of the widget as it appears would be required with your plugin.
Incidently, for anyone who wants both of these features, these two plugins play very nicely together on one of our WP 4.0 Multisite installations with 50+ sites and even on a site using the theme Make.
FYI: We also tested amr shortcode any widget, but had some issues with it and rather than troubleshoot it we moved on. To be fair, we were testing quickly on a site with over 160 plugins active. Yes, 160 plugins. I know, I know, but think about it for a minute. Any plugin that you can activate into that mix of 160 plugins and it does not break things or introduce issues, then it suggests a good probability of it being troublefree to use.... well at least until the next round of WP updates. :)
We also tested Widgetize Pages Light and its pro version Sidebar & Widget Manager and had no issues with either. However, if only seeking to insert widgets into pages and posts these plugins seem like overkill as they provide additional page layout functions that may duplicate theme functions and/or other plugin functions.
I was looking for a "widgets in pages" plugin that provides a TinyMCE button to insert the widget shortcodes.
Can anyone recommend one that has a TinyMCE button to insert the widget shortcodes in pages, posts and CPTs?
Ultimately I selected Widgets On Pages as having the best combination of features for this application. Its simple to understand, easy to use, versatile and has worked flawlessly for us, even along side Widgets Shortcode.
You can do it by wordpress WordPress Widgets Shortcode plugin, you can Embed any widget area/dynamic sidebar to your pages/posts using this plugin as a shortcode
edit widgets in wp :
file in # wp-includes/widgets.php: register_widget()
in source view sample :
public function register( $widget_class ) {
$this->widgets[$widget_class] = new $widget_class();
}

Categories