Risks about edit the source code of a Wordpress plugin - wordpress

I am making my first steps coding. I made some courses on Internet, and now I started to make a Wordpress theme to continue learning from the practice.
I find that there is a lot of Plugins that can help me to achieve the goals that I want, and I also found a plugin that makes almost everything that I want.
I started to modify the source code of that plugin so it could fits in my design scheme. Now I don't know if it is a good idea.
I didn't find a way to make a "child plugin" so at this moment I don't know if continue editing the source of this plugin, (that means that I would never update my plugin because I would lose all the modifications) or simply make everything by my own that would take me a lot more of time.
Do you have some suggestion?

You should take a look into world of hooks and filters.
This is nice place to learn from. Here is list of WP hooks where you can hook your code.
And Woocommerce hook example how to cusom validate field.
First part: woocommerce_checkout_process is place where to hook code and my_custom_checkout_field_process is name of your function.
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
Some plugins have template files, that you can put into child theme.
But sadly not all plugins are well customizable.

Related

Wordpress Woocommerce plugin mail triggering

Mail not sending when status changed from processing to on-hold and processing to failed.
Kindly tell me how to achieve this. Thanks in advance
Even that kind of questions (directly asking the need without any research or any part of code) are not welcomed here, I want to help you about that by giving the basic idea.
In the file "wp-content/plugins/woocommerce/includes/class-wc-emails.php", search for the "public static function init_transactional_emails()" and check "$email_actions" array there.
$email_actions = apply_filters(
'woocommerce_email_actions', array(
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
'woocommerce_new_customer_note',
'woocommerce_created_customer',
)
);
Since after every update of Woocommerce plugin any changes you made on those files will be gone, you need to add your email trigger for status changes you mentioned by either using a hook or overriding the files using your child theme.
About your request, for "from processing to on-hold" you need to add:
'woocommerce_order_status_processing_to_on-hold',
About overriding a file (or function) from includes folder of Woocommerce you may check this post: Override woocommerce files from includes folder
I hope this will help you to solve it. Have a good day.

How to attach files to an email sent by a WordPress booking plugin?

I'm using a WordPress plugin for accepting online bookings (Appointment Hour Booking) and I need to attach a file to the emails sent after submitting the booking request (a PDF file with the general booking terms). I already applied a solution by editing the calls to the wp_mail() function in this way:
wp_mail(trim($payer_email), $subject, $message,
"From: ".$from."\r\n".
$content_type. "X-Mailer: PHP/" . phpversion(),
array(WP_CONTENT_DIR . '/uploads/agreement.pdf'));
The above works but everytime the plugin updates the file is overwritten and I've to reapply the code modification again. There is a better way to do that without being affected by the plugin updates or there is a way to prevent partially or completely a plugin update in WordPress?
Thank you in advance for any help.
Disabling the plugin update isn't a good idea, you may lost important compatibility or security updates. The way the call to the wp_mail() was modified also causes other attachment-related features stop working. The plugin you mention has a filter that can be used to modify the list of attached files, you can put the following code for example into your theme’s functions.php file:
add_filter( 'cpappb_email_attachments', 'my_attach_function', 10, 3 );
function my_attach_function( $attachments, $params, $form_id )
{
$attachments[] = WP_CONTENT_DIR . '/uploads/agreement.pdf';
return $attachments;
}
With the above code located out of the plugin files your file is added to the list of attachments without removing other attachments and locating the code out of the plugin files will prevent being overwritten by the plugin updates.
Your options are:
Fork the plugin and customize it to your needs.
Ask the team behind the plugin to implement a filter hook to allow customizing the headers passed to the wp_mail() function (so you can then attach files to e-mails).
Keep doing what you have been doing until now.
I like option two the best because:
It allows you to customize the behavior of the plugin from the outside, and,
Your changes will survive plugin updates.

How to properly modify 2 lines of another plugins

I am coding some permalinks rewrite for WooCommerce awesome plugin.
Everything is well packed in a plugin and work well... except for one thing.
WooCommerce use get_term_link() to display HTML link in default template. A lot of custom templates use it too. If I want to display the good link on my website, I must change one line of code in woocommerce plugin. This is dirty, since basic user would not do it. Since I want to list it on Wordpress plugin repertory, I would like some help here...
What I need to do :
Open woocommerce.php and replace line 767 :
OLD
$product_category_slug = empty( $permalinks['category_base'] ) ? _x( 'product-category', 'slug', 'woocommerce' ) : $permalinks['category_base'];
NEW
$product_category_slug = '';
Very easy to do by editing the file, but it is dirty. It is non user friendly, and it depends a lot of woocommerce next version... So my question here: how can I do it from my own plugin, without asking people to open their woocommerce plugin with Notepad++... Is there any way ?
I hope you understand my need and could provide me with an answer :)
I already did same thing in Past.
Please Don't Modify WooCommerce Core files, Because it will be gone in your next upgrade.
Use this Custom Permlink Plugin, You will get Better Options there,

How to customize Wordpress Theme before going live

Yesterday I installed a new theme on Wordpress on my self-hosted website. I am aware of the feature that allows you to preview a theme and have used it to select a new Theme that I want to install.
Problem
I do not want to interrupt normal operations of my website, but this new theme requires a lot of customization before it is ready to go. How do I do this?
My Crappy Solution
Is the only way to go about it to run a virtual server on my desktop? This seems tedious, not to mention all the errors I usually get when switching to the "real" server when doing this.
A better way?
I've been searching on SO as well as the WordPress Forum for an answer as to how to do this, but have come up short. I would have thought this is a common question. Maybe I'm using the wrong search terms [themes, customization, before installing]???
Any help is greatly appreciated! Thanks!
Ok, since your question is a pretty good one and probably not a few people are going through the same process when they decide to update their site, I decided to give a try to the get_stylesheet and get_template filter hooks. It turns out that with a very small plugin, you can easily enforce a specific theme(well in this case any logged-in visitor, but you can change this to use any logic you want) according to a specific rule/s.
Here's the code that you need to put in a file in your plugins directory:
<?php
/*
Plugin Name: Switch Theme
Description: Switches the theme for logged-in visitors, while keeping the current theme for everyone else. !!!NOTE!!! Please back-up your database prior using this plugin - I can't guarantee that it will work with any theme, nor that it won't break your site's set-up - USE AT YOUR OWN RISK(I did a quick test and it seemed to be fine, but haven't done extensive testing).
You don't need to switch to the desired theme before that - you want to keep active the theme that you will display to your visitors - the one that you will see will be used programatically.
Before activating the plugin, change the line that says `private $admin_theme = '';` to `private $admin_theme = 'theme-directory-name';` where "theme-directory-name" is obviously the name of the directory in which the desired theme resides in.
*/
class MyThemeSwitcher {
private $admin_theme = '';
function MyThemeSwitcher() {
add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
add_filter( 'template', array( &$this, 'get_template' ) );
}
function get_stylesheet($stylesheet = '') {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $stylesheet;
}
function get_template( $template ) {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $template;
}
}
$theme_switcher = new MyThemeSwitcher();
So - first of all BACKUP YOUR DATABASE! I tested locally with Twenty Eleven being the default theme and a basic framework theme as my custom theme - the theme options and navigation menus were saved properly.
Then all you need to do is to update the file(change the line that says private $admin_theme = ''; to private $admin_theme = 'theme-slug'; where theme-slug is the name of the directory in which the theme you want to use is).
Also - you won't be able to change the Front page and Posts page options, without this affecting the live site, nor will you be able to change the any shared components that both themes use(Site name, Front Page, Posts page, Posts Per Page, etc options, content and so on).
So if you have no clue whether this solution is for you - well, it depends.
If both themes are not relatively complex, then most-likely you should be able to use this hack. If they are though maybe you should do a second installation of your website as others suggested - I think that a second installation in either a sub-domain or a sub-directory would be the best option for you(simply because moving a multisite database is more complex than moving a normal WP database).
I'd setup local apache server with a wordpress installed to customize and test a new theme. When you finished customizing it then you can upload the theme to your live site and activate it. If there are settings that you need to set in the dashboard then you probably will have to adjust them again. That's one way to test/customize a theme before putting it live.
You could create a network (make WordPress multisite with define('WP_ALLOW_MULTISITE', true);, see : http://codex.wordpress.org/Create_A_Network) and then create one sub-site, then turn it "off" with a Maintenance plugin so it is not accessible to users not logged in as admin, export your posts & data from main blog, import them in sub-blog with WordPress default importer, then apply your new theme to this sub-blog and work on it. When everything satisfies you, apply the theme to the main site and deactivate subsite.

Insert a plugin manually into wordpress page

I am working in worpress front page.
I want to add a plugin to the page at a specific location manually but adding the code to the page myself.
I basically want to include a plugin in a certain page on a certain location. So I'm create a div...
<div id="plugin-holder">
**Plugin-will-appear-here-with-this-code**
</div>
Don't anyone know how this is done please?
Thanks
If you're wanting a plugin to appear somewhere, you'll be looking for "shortcode" functionality.
This is actually surprisingly easy to code, check out the examples in the Codex under Shortcode API - ie:
function bartag_func( $atts ) {
// ... do more things here ...
return "text to replace shortcode";
}
add_shortcode( 'bartag', 'bartag_func' );
Once you've called these functions you can use [bartag] in code and it will run your function and replace the shortcode with the generated text your function returns.
If you're adding shortcode functionality to your site, it generally makes most sense to code a really simple plugin and put it in that. The reason why this works best is that, over time, it's really easy to forget and upgrade a theme by mistake (or even change to a new theme) and thus break your site by losing your custom code in your former functions.php. Surprisingly, this is pretty easy to achieve and only requires some specially formatted comments at the top of your plugin file and a little common sense in coding - there are many tutorials and "how to"s around!
Here's a useful shortcode tutorial: http://www.reallyeffective.co.uk/archives/2009/06/22/how-to-code-your-own-wordpress-shortcode-plugin-tutorial-part-1/
You should add the relevant plugin code to functions.php.
I suspect you'll want to use some conditional tags, like is_home() to pinpoint your location. But maybe not, depending on what you are trying to do,
Also, if you're trying to to insert from a pre-existing plug-in, make sure you remove the register_activation_hook or activate_pluginname action.
If your plugin supports a sidebar widget you can simply "widgitize" the div tag that you wish to insert the plugin into.. Google the term and you are gonna find many resources.

Resources