Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#45377 new defect (bug)

Video Shortcode Ajax Issue

Reported by: nenad-obradovic's profile Nenad Obradovic Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version: 5.0
Component: Shortcodes Keywords:
Focuses: Cc:

Description

Hi guys,

I have the problem with wp_video_shortcode element settings on ajax trigger, where defaults_atts variable override settings I forward to shortcode. Because is_admin() conditional inside shortcode function return true on ajax calling.

Example code

<div class="video-player-element">
	<?php
	// Get video meta src
	$video_meta = get_post_meta( get_the_ID(), 'video_player_src', true );

	// Video player settings
	$settings = apply_filters( 'video_player_settings', array(
		'width'  => 1300, // Aspect ration is 16:9
		'height' => 731,
		'loop'   => true
	) );
	
	// Init video player
	echo wp_video_shortcode( array_merge( array( 'src' => esc_url( $video_meta ) ), $settings ) ); ?>
</div>

When I call ajax to load the new content on frontend, video player element is loaded with my settings but on rendering video, wp_video_shortcode function override my settings with default because function enters inside this conditional

if ( is_admin() ) {
	// shrink the video so it isn't huge in the admin
	if ( $atts['width'] > $defaults_atts['width'] ) {
		$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] );
		$atts['width'] = $defaults_atts['width'];
	}
}

Is there any solutions to skip this check, or if you add some filter around defaults_atts so I can be able to change that values? Thanks for your solutions

Best regards,
Nenad

Change History (5)

#1 @swissspidy
6 years ago

As the name implies, admin-ajax.php runs in admin context. I'd recommend looking into using the superior REST API instead. For example, WordPress 5.0 has a WP_REST_Block_Renderer_Controller class that you could use as inspiration for a shortcode renderer controller.

#2 @Nenad Obradovic
6 years ago

Hi swissspidly,

Thanks for your response. This might be one solution, but if anyone what to use admin-ajax instead of Rest API then problem will still exist.

Is it possible to add some hook around defaults_atts and we can fix this problem with simple hook when we use ajax-admin, example code

/**
 * Filters the default video shortcode attributes.
 *
 * @see wp_video_shortcode()
 *
 * @param array  $defaults_atts     Default attributes of the shortcode. @see wp_video_shortcode()
 */
$defaults_atts = apply_filters( 'wp_video_shortcode_defaults_atts', $defaults_atts );

Best regards,
Nenad

#3 @Nenad Obradovic
6 years ago

Hi swissspidly,

Because if I understand the purpose of this conditional, that you set this case in order to set smaller video player inside TinyMCE editor and not for the frontend area

Best regards,
Nenad

#4 @lonelyvegan
6 years ago

  • Milestone changed from Awaiting Review to Future Release

Moving this out of 5.0 bugs needing review, but from the looks of it the issue stems from using admin-ajax.php inappropriately (outside of an admin context), right? Seems like this should be a won't fix.

#5 @Nenad Obradovic
6 years ago

Hi lonelyvegan,

I think no, I used default js ajax function for the front-end

$.ajax({
	type: 'POST',
	data: ajaxData,
	url: $ajaxUrl,
	success: function (data) {
		
	}
});

where $ajaxUrl is esc_url( admin_url( 'admin-ajax.php' ) );

Best regards,
Nenad

Note: See TracTickets for help on using tickets.