Making Changes to MarketPress Templates

Hi,

I've been spending a bit of time reading up on customising MarketPress the past few days, including the posts on the topic on this site, as an upcoming project is likely to require some customisation of MarketPress templates.

Specifically what I need to do right now is add additional, custom meta to the product listings and product pages after the product excerpt. Ideally I don't want to be creating whole new page templates just for this, as overall I'm happy with the default layouts provided by the plugin.

I was hoping to find some sort of add_action( 'mp_after_excerpt' , 'my_function' ); but can't find anything like this. At the moment, I've played around using filters, e.g. add_filter( 'the_excerpt' , 'my_function' , 10 ) and adding my custom meta HTML on the end. It works OK, but I'm a little uncomfortable as I need to add a closing </div> at the start ( to close the div containing the excerpt ) and leave out my closing </div> so it can be closed by the existing template.

Looking for some input here, is the way I'm currently working reasonable or is there a better way?

Thanks,

  • Adam Czajczyk

    Hello Domnic,

    I hope you're well today and thank you for your question!

    The MarketPress products are actually custom posts (see "custom post types") so they are supported and handled by WordPress "out of the box" mostly the same way as all other posts. Therefore hooking to "the_excerpt" is a proper way to deal with MarketPress products excerpt.

    You may however implement additional "post type" check inside your callback function to make sure that only the products are affected and all other posts remain intact (or are handled yet another way).

    As it comes to the "</div>" tag. If I understand you correctly, the issue here is that "the_excerpt" hook by default filters out "html", am I right? I think you could then either use

    remove_filter() function to remove default filters from "the_excerpt"

    before adding your own code but the better way would be to target "get_the_excerpt" instead of "the_excerpt". Take a look here please:

    https://codex.wordpress.org/Plugin_API/Filter_Reference/get_the_excerpt

    and at this thread at WP forum:

    https://wordpress.org/support/topic/calling-get_the_excerpt-from-a-filter-on-the_content-kills-shortcodes.

    That said, there's a "mp_get_the_excerpt()" function, however there's actually no reason to hook into it as after all it applies the "the_excerpt" filter to it's output so interacting with "the_excerpt" or "get_the_excerpt" WP native filters would be kinda "low level" coding, which is - I think - a better solution

    I hope that helps!

    Best regards,
    Adam

  • Dominic

    Hi,

    Thanks for getting back to me, that's not quite what I'm asking though. I don't want to change or filter the excerpt itself, I'm trying to add additional content / block of HTML after the excerpt on the product page. And similarly after the '.mp_product_details' container on the product list.

    The reason I mentioned the closing div tags is, if you look in the file /marketpress/includes/common/template-functions.php at the function mp_product() ( used to display the product page ), it pulls the excerpt on lines 2446-2448, i.e.

    $return .= '<div class="mp_product_excerpt">';
    $return .= mp_get_the_excerpt( $product_id, apply_filters( 'mp_get_the_excerpt_length', 18 ) );
    $return .= '</div><!-- end mp_product_excerpt -->';

    I want to add content AFTER the excerpt not in it. Therefore I've needed to kick my function attached to the filter off with a closing div ( to close ".mp_product_excerpt" ), and leave one off at the end ( as it'll be closed for me by line 2448.

    So, the real question is, what is the best way to add additional content after the excerpt on the product page, and after the '.mp_product_details' container in the product list?

    Thanks,

  • Ivan

    Hi Dominic !

    For product list, you can try this code

    function change_mp_products_html_( $html, $custom_query ) {
        $your_html_code = '<b>your_html_code</b>';
        $place = '<!-- end mp_product_details -->'; //for adding after the '.mp_product_details' container
        $html = str_replace( $place, $place . $your_html_code, $html );
        return $html;
    }
    
    add_filter('_mp_products_html_grid', 'change_mp_products_html_', 10, 2 );
    add_filter('_mp_products_html_list', 'change_mp_products_html_', 10, 2 );

    For the product page in Admin area, you can try this code

    function add_mp_custom_meta($post) {
        if( class_exists( 'WPMUDEV_Metabox') ) {
            $metabox = new WPMUDEV_Metabox( apply_filters( 'mp_metabox_array_mp-custom-metabox', array(
    			'id'        => 'mp-custom-metabox',
    			'title'     => __('Custom Fields'),
    			'post_type' => MP_Product::get_post_type(),
    			'context'   => 'normal',
                            'priority'     => 'high',
    		) ) );
            $metabox->add_field( 'select', array(
    			'name'          => 'some_name',
    			'id'            => 'mp-some_name',
    			'default_value' => '1',
                            'options'       => array('1'=> 'one', 2 => 'two', 3 => 'three'),
    		) );
        }
    }
    add_action('dbx_post_advanced', 'add_mp_custom_meta');

    More examples you can see in this file

    marketpress/includes/admin/store-settings/class-mp-store-settings-presentation.php

    It seems this block will be added before Excerpt block because WP doesn't use hard priority for metaboxes.

    You can add this code as MU plugin - create "add_mp_custom_meta.php" file to {your_wordpress}/wp-content/mu-plugins/ via FTP.

    Please let me know how it goes.

    Best regards,
    Ivan.