1

I asked this on the WordPress Plugins/Hacks Forum because SE was in read-only mode yesterday. I did get an answer, but the "relative noobie" is kicking in, as I need an example in order to see how to use the methods the answer provided.

Original Post and Answer

Reposted below so example will fit...


I'm relatively new to WordPress, but I'm a programmer for a living. Because of that, I don't like reinventing the wheel. Recently, I downloaded a plugin. I can modify this post to say what the plugin was if needed, but this is more generalized. This plugin tracks items, and creates newsletters based on those tracked items.

One of those items is Gender. Since the visitors I get are sometimes couples, they share email addresses(email is a required field), therefore I cant demand of them to each have an email, so in the Gender Field, I added Couple, tested it, and it works great. It now adds an m for male, f for female, and a c for couple to the plugin's customized table in the WordPress Database.

Recently, the plugin had an update, and I updated it. Because I modified pieces in the plugin's core files, the update overwrote the custom field I added, causing me to have to re-add the custom work I did. Can someone tell me what the best approach is to customizing the plugin, while keeping it up to date? Is there a hook I can use/add that will accomplish what I'm asking etc.?


I did find this:

How to edit a wordpress plugin without break its update process, the answer by Tom, is the one I need to know if I can implement, and how...

UPDATE
Plugin Name: Newsletter

Example Edit:

wp-content/plugins/newsletter/users/edit.php

<tr valign="top">
<th>Gender</th>
   <td>
      <?php $controls->select('sex', array('n' => 'Not specified', 'f' => 'Female', 'm' => 'Male', 'c' => 'Couple')); ?>                             
       <div class="hints">                
            This can also be a couple, if our members are sharing e-mail addresses.
       </div>
       </td>
       </tr>
10
  • Tom lays it out for you, those are pretty much your options. You should NEVER, EVER edit a plugin's core files for the very reason that brought you here. Sky will fall. We need to know what the plugin is to help you, although plugins can usually be off-topic, your problem and question is more technically related to the extension of the aformentioned so you'll be ok to mention the plugin name and link here. We need to look at the code to see what the plugin offers you, then we can help further.
    – Adam
    Commented Jun 9, 2014 at 2:53
  • @userabuser, added UPDATE Section. Everywhere, the author referred to the sex_table, I added Couple, this took me about 4 files in all kinds of different places, as I modified a database value etc
    – eyoung100
    Commented Jun 9, 2014 at 3:20
  • What does your update clause look like?
    – Adam
    Commented Jun 9, 2014 at 3:42
  • In the subscription.php, edit.php, profile.php and a few others, I added sex_couple. Let me see if I can find the mods...
    – eyoung100
    Commented Jun 9, 2014 at 4:19
  • You will need to create a child theme to do your customizations in. You should never modify core files, or any theme/plugin that you are not the author of for the reason of what just happened to you. There are a lot of tutorials on how to create a child theme. Hope this help Commented Jun 9, 2014 at 4:29

1 Answer 1

2

You have two options I can see after looking at the plugin, although there may be other ways to tackle this my time is limited so I'll give you some insight on what you can do now, in the interim.

First of all the plugin author provides you some additional filters:

  • newsletter_profile_extra
  • newsletter_subscription_extra
  • newsletter_profile_save

(see newsletter/subscription/subscription.php)

You can use these filters to add additional controls to the appropriate subscription and profile pages.

However, note that from a glance, there does not appear to be any filters to augment the existing fields such as the sex/gender field.

But... What you can do is use these filters to add an extra field like a checkbox that asks the user whether they are a couple and if they mark this checkbox you can use the third filter newsletter_profile_save to filter the values before they are inserted into the database.

At this point, you would determine that IF the checkbox is marked then you replace the sex post value with "couple" or "c" or whatever the database value you wish to be stored for later retrieval:

//pseudo-code example
if ( isset($_POST['are_you_a_couple']) && $_POST['are_you_a_couple'] === 1 ) {
   $_POST['sex'] = 'couple';
}

The process described above is a high level overview only and is far more work than my next proposed solution which is to enqueue your own, custom JavaScript file on the appropriate pages where the form controls show and add the additional option to the sex/gender drop down field.

An example would be to add the following code to your theme functions.php file:

/**
 * Proper way to enqueue scripts and styles
 */
function custom_newsletter_scripts() {
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/custom-control.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'custom_newsletter_scripts' );

Then create you custom-control.js file in your theme placed under the /js/ directory or wherever you wish to store the script, just ensure your path reflects that in your call to wp_enqueue_script above.

In your JavaScript file you may do something to this effect:

;(function($){

  $(document).ready(function(){

     $('#gender_select_menu')
         .append($("<option></option>")
         .attr("value",'c')
         .text('Couple')); 

  });

})(jQuery);

Code example is rough, execute your JavaScript as per your liking, if the control does not exist at the point of DOMReady you might wish to add a timeout or modify the DOM on $(window).load(function(){});

This should be enough to get you going in the right direction and keep your changes future proof from any further loss on plugin updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.