0

I'm trying to replace a function in a WordPress plugin (Easy property listing) without success at the moment.

The action is added like that, in a class :

<?php
/**
 * Search Object
 *
 * @package     EPL
 * @subpackage  Classes/Search
 * @copyright   Copyright (c) 2016, Merv Barrett
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.0
 */
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * EPL_Search_Fields Class
 *
 * @since      3.0
 * @author     Taher Atashbar <taher.atashbar@gmail.com>
 */
class EPL_Search_Fields {
    /**
     * Initialize hooks.
     *
     * @since  3.0
     * @return void
     */
    public function init() {
        // Initialize hooks for displaying search frontend fields.
        add_action( 'epl_frontend_search_field_text', array( $this, 'render_text' ), 10, 5 );
        add_action( 'epl_frontend_search_field_checkbox', array( $this, 'render_checkbox' ), 10, 5 );
        add_action( 'epl_frontend_search_field_select', array( $this, 'render_select' ), 10, 5 );
        add_action( 'epl_frontend_search_field_multiple_select', array( $this, 'render_multiple_select' ), 10, 5 );
        add_action( 'epl_frontend_search_field_number', array( $this, 'render_number' ), 10, 5 );
        add_action( 'epl_frontend_search_field_hidden', array( $this, 'render_hidden' ), 10, 5 );
        add_action( 'epl_frontend_search_field_radio', array( $this, 'render_radio' ), 10, 5 );
        add_action( 'epl_frontend_search_field_checkbox_multiple', array( $this, 'render_checkbox_multiple' ), 10, 5 );
    }

View the file on Github

The function I tried to replace is render_select. Further down in the file :

/**
     * Renders search frontend Select field.
     *
     * @since  3.0
     * @param  array  $field
     * @param  string $config
     * @param  string $value
     * @param  string $post_type
     * @param  string $property_status
     * @return void
     */
    public function render_select( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
        if ( isset( $field['wrap_start'] ) ) {
            echo '<div class="' . $field['wrap_start'] . '">';
        }
        ?>
        <div class="epl-search-row epl-search-row-select epl-<?php echo $field['meta_key']; ?> fm-block <?php echo isset( $field['class'] ) ? $field['class'] : ''; ?>">
            <label for="<?php echo $field['meta_key']; ?>" class="epl-search-label fm-label">
                <?php echo apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ); ?>
            </label>
            <div class="field">
                <select
                    name="<?php echo $field['meta_key']; ?>"
                    id="<?php echo $field['meta_key']; ?>"
                    class="in-field field-width">
                    <option value="">
                        <?php echo apply_filters( 'epl_search_widget_option_label_' . $field['option_filter'], __( 'Any', 'easy-property-listings'  ) ); ?>
                    </option>
                    <?php
                    if ( isset( $field['options'] ) && ! empty( $field['options'] ) ) {
                        foreach ( $field['options'] as $k => $v ) {
                            echo '<option value="' . esc_attr( $k ) . '"' . selected( $k, $value, false ) . '>' . $v . '</option>';
                        }
                    }
                    ?>
                </select>
            </div>
        </div>
        <?php
        if ( isset( $field['wrap_end'] ) ) {
            echo '</div>';
        }
    }

I'm trying to remove the first option value in a select input in my search bar :

<option value="">
    <?php echo apply_filters( 'epl_search_widget_option_label_' . $field['option_filter'], __( 'Any', 'easy-property-listings'  ) ); ?>
</option>

So I just copied this render_select function without the "option" in a plugin, and I named it "render_select2" and it's working. I doubled my select input without the first option value.

Now i try to remove the initial action. I tried a lot of code (on this forum and in some post on Google) without success.

In my functions.php :

function remove_searchfield() {

    $class = 'EPL_Search_Fields'; // Name of class the method was built in.
    $tag = 'epl_frontend_search_field_select'; // the name of the hook
    $function = 'render_select'; // the function you wish to remove
    $priority = 10; // must be an integer

    global $class;
    remove_action ( $tag, array( $class, $function ), $priority);
}
add_action( 'init', 'remove_searchfield');

Do you guys have an idea how to do that? My PhP knowledge is limited and I'm going crazy ;).

Any tips any advice would be welcome.

Thank you.

  • 1
    It depends on how EPL_Search_Fields is instantiated. render_select() isn't static, so you need to pass the existing instance of EPL_Search_Fields to remove_action() to be able to remove it. – Jacob Peattie Apr 28 at 12:30
  • Thank you Jacob, I'm not sure how to do that, I find an occurence of the class in an other file : Github – RomainJM Apr 28 at 13:09
1

I finally managed to make it work :

function remove_searchfield() {

    $tag = 'epl_frontend_search_field_select'; // the name of the hook
    $function = 'render_select'; // the function you wish to remove
    $priority = 10; // must be an integer

    remove_action ( $tag, array( Easy_Property_Listings::instance()->search_fields, $function ), $priority);
}
add_action( 'init', 'remove_searchfield');

Thanks Jacob for the help ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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