Support » Developing with WordPress » How to define a filter in plugin before plugin load?

  • Hi all,

    I have the functions code below in plugin:

    if( !function_exists('tourmaster_get_enquiry_form') ){
    	function tourmaster_get_enquiry_form( $post_id = '' ){

    And I want add filter for it like this:

    if( is_admin() ){ add_filter('the_enquiry_form', 'tourmaster_get_enquiry_form'); }
    	if( !function_exists('tourmaster_get_enquiry_form') ){
    		function tourmaster_get_enquiry_form( $post_id = '' ){

    But the filter not work :(, I’ve tried ask the dev and he told me I must define the filter before the plugin load to make it work. It’s quite difficult for me because I don’t familiar with PHP and WordPress Codex. I hope I can get some advice to learn about it.

    Thank to advance for any help!!!

    • This topic was modified 2 months, 1 week ago by Loc_rabbirt.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Are you trying to redefine the plugin’s function or just add a filter to the plugin’s function?
    Is the plugin applying that filter?
    There is not a way in WordPress to tell it which plugin to load first. It it typically alphabetical by folder name. Actually, I’ve seen a plugin that affects the loading order of plugins, but mostly not. The mu-plugins are loaded first, though.

    If you want to redefine the function, yours needs to load before the other plugin does.
    If you want to filter data from the plugin’s function (that is already calling apply_filters), you simply put the add_filter call in your plugin with whatever priority makes sense in regards to the filter (not to the loading).

    @joyously Thank you so much for your help, but I’m a foolish student when come to WordPress Codex, so I’m not sure if the apply_filters call or not. How I can check it? I don’t think apply_filters avaiable for this function yet, so I think I must apply_filters for it first before I can do more.

    You haven’t said what you are trying to accomplish, or what the original plugin code does. You really should work this out with the plugin developer. There’s no good way for an outsider to know what’s going on.
    Filters are only called using apply_filters(). You can add any number of filters to the list with add_filter(), but the code is not executed until apply_filters().

    I have the default code below:

    // enquiry form
    if( !function_exists('tourmaster_get_enquiry_form') ){
        function tourmaster_get_enquiry_form( $post_id = '' ){
    
            if( !empty($post_id) ){
                $custom_fields = get_post_meta($post_id, 'tourmaster-enquiry-form-fields', true);
            }
            if( empty($custom_fields) ){
                $custom_fields = tourmaster_get_option('general', 'enquiry-form-fields', '');
            }
    
            if( empty($custom_fields) ){
                $enquiry_fields = array(
                    'full-name' => array(
                        'title' => esc_html__('Full Name', 'tourmaster'),
                        'type' => 'text',
                        'required' => true
                    ),
                    'email-address' => array(
                        'title' => esc_html__('Email Address', 'tourmaster'),
                        'type' => 'text',
                        'required' => true
                    ),
                    'your-enquiry' => array(
                        'title' => esc_html__('Your Enquiry', 'tourmaster'),
                        'type' => 'textarea',
                        'required' => true
                    ),
                );
            }else{
                $enquiry_fields = tourmaster_read_custom_fields($custom_fields);
            }
    
            $ret  = '<form class="tourmaster-enquiry-form tourmaster-form-field tourmaster-with-border clearfix" ';
            $ret .= ' id="tourmaster-enquiry-form" ';
            $ret .= ' data-ajax-url="' . esc_url(TOURMASTER_AJAX_URL) . '" '; 
            $ret .= ' data-action="tourmaster_send_enquiry_form" ';
            $ret .= ' data-validate-error="' . esc_attr(esc_html__('Please fill all required fields.', 'tourmaster')) . '" ';
            $ret .= ' >';
            foreach( $enquiry_fields as $slug => $enquiry_field ){
                $enquiry_field['echo'] = false;
                $enquiry_field['slug'] = $slug;
                
                $ret .= tourmaster_get_form_field($enquiry_field, 'enquiry');
            }
    
            $recaptcha = tourmaster_get_option('general', 'enable-recaptcha', 'disable');
            if( $recaptcha == 'enable' ){
                $ret .= apply_filters('gglcptch_display_recaptcha', '', 'tourmaster-enquiry');
            }
    
            $our_term = tourmaster_get_option('general', 'register-term-of-service-page', '#');
            $our_term = is_numeric($our_term)? get_permalink($our_term): $our_term; 
            $privacy = tourmaster_get_option('general', 'register-privacy-statement-page', '#');
            $privacy = is_numeric($privacy)? get_permalink($privacy): $privacy; 
            $ret .= '<div class="tourmaster-enquiry-term" >';
            $ret .= '<input type="checkbox" name="tourmaster-require-acceptance" />';
            $ret .= sprintf(wp_kses(
                __('* I agree with <a href="%s" target="_blank">Terms of Service</a> and <a href="%s" target="_blank">Privacy Statement</a>.', 'tourmaster'), 
                array('a' => array( 'href'=>array(), 'target'=>array() ))
            ), $our_term, $privacy);
            $ret .= '<div class="tourmaster-enquiry-term-message tourmaster-enquiry-form-message tourmaster-failed" >' . esc_html__('Please agree to all the terms and conditions before proceeding to the next step', 'tourmaster') . '</div>';
            $ret .= '</div>';
    
            $ret .= '<div class="tourmaster-enquiry-form-message" ></div>';
            $ret .= '<input type="hidden" name="tour-id" value="' . get_the_ID() . '" />';
            $ret .= '<input type="submit" class="tourmaster-button" value="' . esc_html__('Submit Enquiry', 'tourmaster') . '" />';
            $ret .= '</form>';
    
            return $ret;
        }
    }

    And I want remove this part of code in the function above:

    $our_term = tourmaster_get_option('general', 'register-term-of-service-page', '#');
    $our_term = is_numeric($our_term)? get_permalink($our_term): $our_term; 
    $privacy = tourmaster_get_option('general', 'register-privacy-statement-page', '#');
    $privacy = is_numeric($privacy)? get_permalink($privacy): $privacy; 
    $ret .= '<div class="tourmaster-enquiry-term" >';
    $ret .= '<input type="checkbox" name="tourmaster-require-acceptance" />';
    $ret .= sprintf(wp_kses(
        __('* I agree with <a href="%s" target="_blank">Terms of Service</a> and <a href="%s" target="_blank">Privacy Statement</a>.', 'tourmaster'), 
        array('a' => array( 'href'=>array(), 'target'=>array() ))
    ), $our_term, $privacy);
    $ret .= '<div class="tourmaster-enquiry-term-message tourmaster-enquiry-form-message tourmaster-failed" >' . esc_html__('Please agree to all the terms and conditions before proceeding to the next step', 'tourmaster') . '</div>';
    $ret .= '</div>';

    The default function don’t have filter, so I just add filter but not sure where to apply filter for it.

    Since the original is pluggable (it has the if statement checking for existence of the function), then like the developer said, you just have to make sure your version is loaded before the original.
    Yours should also have the if statement, so copy the original exactly, put it in your plugin (not theme since plugins are loaded before themes) that has a folder name alphabetically before the original, and then chop out the part you don’t want.
    If that doesn’t work to run yours instead of theirs, put yours into a Must Use plugin, which means you make a folder called mu-plugins at the same level as plugins, and put your file in there. Must Use plugins load before normal plugins.

    If you still can’t get it working, go back to the developer and suggest that they use filters instead of pluggable functions.

    @joyously my sincerely thank to you, I will take a look in mu-plugins and deal with it. Honestly, the dev and me are both colleague, but we have to deal different tasks, as my-self, I must learn by my-self, because he can’t teach me (I think he have a long line list to handle each day).

    Have a sweet day!!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.