Skip Navigation

[Gelöst] Views Shortcode not loading within other plugins that support shortcode

This support ticket is created vor 7 Jahre, 1 Monat. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 11 Antworten, has 2 Stimmen.

Last updated by Nigel vor 7 Jahre, 1 Monat.

Assisted by: Nigel.

Author
Artikel
#484373
Detailed settings ‹ TechWomen — WordPress.png

Hello,

There are two different, but somewhat related, issues here that I wanted to ask about. Basically, I have this world map plugin that allows a user to show and hide content depending on which country is clicked. This map plugin supports shortcode, and I have used my own custom shortcode to show the content I need.

I attempted to use a view shortcode here and I simply get the shortcode text and my view doesn't render at all. I'm curious what could be the reason for that.

My second question relates to registering custom post types and taxonomies. I built a custom WordPress query in my shortcode that grabs matching results of the post type "Impact Stories" from the taxonomy "country" and outputs it below the map. I realized that setting up a custom post type and taxonomy within Toolset wasn't sorting by country as I had expected. However, manually setting up the post type and taxonomy in php yielded successful results. My question then is why would my manual post type and taxonomy registration work, but the Toolset post type and taxonomy fail? Does it have to do with the way those things are registered?

I'm happy to setup a login on the test version of my site if you wanted to do some more investigating. You can check out the attached screenshot to see where I included the shortcode on my map plugin. You can visit this page to see the map on the front end. Right now I've included my custom shortcode as well as a view shortcode. You can click on the United States to see the content load in: hidden link.

#484456

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kristin

Regarding the first question, Views uses a custom shortcode handler (which, I believe is to handle nested shortcodes, including the ability to use shortcodes as attributes in other shortcodes), and I suspect there may be a conflict with the shortcode handling of your maps plugin.

I would need a copy of the plugin for testing and to be able to pass the compatibility team to identify how they can work together.

Regarding the second question, that is something of a puzzle.

As far as I understand Types registers the custom post types and custom taxonomies using the standard WordPress functions (such as register_post_type) that you would use if registering them manually, which suggests the most likely explanation would be different settings used when registering them.

I would need to compare how you register them in Types and compare that with how you register them manually, and then see how and where you are using them to try and replicate the issue.

I don't fully understand "I realized that setting up a custom post type and taxonomy within Toolset wasn't sorting by country as I had expected". WordPress is quite opinionated about taxonomies—that they are for grouping—whereas custom fields are for ordering, and you would need custom SQL to order by taxonomy terms.

Looking at the page you linked to, I understood you would want your query to return "Impact stories" with the country term coming from the map according to where it was clicked. Can you elaborate a little?

I will mark your next reply as private so that I can get site credentials so that I can get a copy either of the map plugin itself, or possibly the whole site that already has it set up, to investigate the compatibility issue.

You may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make any changes other than to temporarily add a backup plugin.

#484975

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kristin

I set up a test site using your maps plugin and was able to reproduce the problem that the wpv-view shortcode is not being processed and is simply printed.

An outright fix may not be possible because our shortcode is wrapped inside their shortcode, however, I did come up with a workaround.

Standard shortcodes do work in the plugin editor, so I registered a custom shortcode like so:

add_shortcode( 'render-view', function($atts){
  
  $view = render_view( array( 'id' => $atts['id']) );
  return $view;
});

Now, in place of the wpv-view shortcode I used [render-view id="xx"], supplying the id of the view, and that worked.

As for your second issue I wasn't really able to test it as there is only a single post, so now ordering or grouping to observe or test.

#485186

Thanks, using the render_shortcode function worked for outputting the view. However, using a custom view attribute to filter by country doesn't work when I call my custom shortcode by clicking on a particular country. I have the US and Canada set so that they should filter view results, but they're both returning all results. See my screenshot above where I add the shortcode content the the 'Detailed Settings' field in the World Map plugin. Here's the code I'm using to set this up:

/**
* Build and return the Impact Stories component.
*
* @since 1.0.0
*
* @param array $args The args.
*
* @return string The HTML.
*/
add_shortcode( 'twm_map', 'twintmap' );

function twintmap( $args ) {

$defaults = array(
'country' => '',
);

$view_args = wp_parse_args( (array)$args, $defaults );

$country = sanitize_text_field( $args['country'] );

$args = array(
'title' => 'Impact Stories',
'country' => $country
);

echo render_view( $view_args );

}

#485369

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kristin

Without any testing, looking at your code you seem to be confusing or juxtaposing $args and $view_args...

add_shortcode( 'twm_map', 'twintmap' );
function twintmap( $args ) {

	$defaults = array(
		'country' => '',
	);

	$view_args = wp_parse_args( (array)$args, $defaults );
	// probably gives $view_args = array( 'country' => 'united-states' ), yes?

	$country = sanitize_text_field( $args['country'] );
	// $args or $view_args which you set up in the last step?

	$args = array(
		'title' => 'Impact Stories',
		'country' => $country
	);
	// why define $args here, you don't use it anywhere?

	echo render_view( $view_args );
	// did you really mean $view_args?
}

Try fixing that, and double check that your country slug here corresponds with the taxonomy country slug.

#485564

Yes I checked my code and tested. The custom shortcode works just fine outside of the context of the map. for instance, if I just insert [twm_map country="united-states"] on any other page using the regular WordPress wysiwyg, it outputs as expected.

#485567

I should also mention that registering this taxonomy manually filters just fine with this shortcode. I've set that up with this code:

//Create a custom taxonomy.
add_action( 'init', 'twm_taxonomy' );
function twm_taxonomy() {

// Add new taxonomy.
$labels = array(
'name' => _x( 'Impact Story Countries', 'taxonomy general name' ),
'singular_name' => _x( 'Impact Story Country', 'taxonomy singular name' ),
'search_items' => __( 'Search Impact Story Countries' ),
'all_items' => __( 'All Impact Story Countries' ),
'parent_item' => __( 'Parent Impact Story Country' ),
'parent_item_colon' => __( 'Parent Impact Story Country:' ),
'edit_item' => __( 'Edit Impact Story Country' ),
'update_item' => __( 'Update Impact Story Country' ),
'add_new_item' => __( 'Add Impact Story Country' ),
'new_item_name' => __( 'New Impact Story Country' ),
'menu_name' => __( 'Impact Story Countries' ),
);

// Now register the taxonomy
register_taxonomy(
'impact-story-country',
'impact-story',
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'impact-story-country' ),
)
);
}

I'll disable this for now and use the Toolset version of the taxonomy ( titled Impact Story Country ) so you can see it not working. If you guys could check out the site with the login info I gave you, and give some insight as to why creating the taxonomy within toolset doesn't work the same, it would be much appreciated. Thanks!

#486161

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kristin

Before turning to the question about the taxonomy, if your custom shortcode works it appears to be working by accident.

You can read about the render_view function here: https://toolset.com/documentation/user-guides/views-api/

In your code you use render_view( $view_args ), where $view_args is array ( 'country' => 'united-states' ).

But the arguments for render_view needs to include the title or slug or id of the View, which is missing.

You add the title "Impact Stories" to the array $args, which you don't then use.

You need to call render_view with an array that identifies the view as well as passes the country attribute.

Once you have done that it will be possible to consider whether the view is rendering as expected or not.

If you could make that change and see what happens, then I can consider the taxonomy issue.

#486393

Hey,

Yes I fixed that juxtaposition of $args and $view_args in my code. No change to the results filtering based on country taxonomy though. Here's my code now:

add_shortcode( 'twm_map', 'twintmap' );

function twintmap( $args ) {

$defaults = array(
'country' => '',
'title' => 'Impact Stories'
);

$args = wp_parse_args( (array)$args, $defaults );

$country = sanitize_text_field( $args['country'] );

$view_args = array(
'title' => 'Impact Stories',
'country' => $country
);

echo render_view( $view_args );

If you guys could look more in to the taxonomy issue that would be much appreciated. Thanks!

#486651

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kristin

I looked through the Types source code at where taxonomies are registered and I can't see anything that would affect the ordering differently from when you register the taxonomy manually.

(If you want to check yourself the code is in the file wp-content/plugins/types/library/toolset/types/embedded/includes/custom-taxonomies.php with the function wpcf_custom_taxonomies_register defined from line 148.)

You don't specify all arguments when registering the taxonomy manually, but the ones you omit that may be set by Toolset don't appear significant.

But I can't see the problem on your site because there isn't the content to sort. There are only 2 impact stories.

Where are you seeing the problem?

#486766

Check out this page and try clicking on the United States within the map: hidden link. You'll see it outputs both US and Canada results, where the shortcode hooked up to it is supposed to filter just US results. I attached a screenshot about showing where that shortcode is input in the maps plugin.

#487064

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Kristin

Sorry for the misunderstanding, I thought the issue was with sorting by taxonomy, rather than filter by taxonomy.

I can't account for why it seems to work when you manually register the post type but not when you register the post type using Types, as I set up a local test site to try something similar and it worked with a Types custom post type.

I registered a custom post type "Contents" and assigned a custom taxonomy "Status" to it, then created a View (id = 42) to display Contents with a taxonomy filter where the taxonomy term is supplied by a shortcode attribute "status".

My crude shortcode looks like this:

add_shortcode( 'show-view', function( $atts ){

	$atts = shortcode_atts( ['status' => ''], $atts );

	$args = array(
		'id'	=> 42,
		'wpvstatus'	=> $atts['status']
	);

	return render_view( $args );

});

And then when I add the following to a page I see the results.

[show-view status="active"]

That shows only the Contents with a taxonomy status of 'active', as expected.

So, I re-visited the map plugin.

I added the shortcode to the details section for the United States that is revealed when clicking on the map, and I found the same as yourself, that all results were returned and the shortcode attribute was ignored.

I reported the issue with the wpv-view shortcode simply being printed to the screen and was advised that there is nothing we can do in situations where Views shortcodes are embedded inside 3rd party shortcodes.

How many countries are involved? You may need to create a separate View for each country you require.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.