Piense en un contenido de publicación como el siguiente:
[shortcode a="a_param"]
... Some content and shortcodes here
[shortcode b="b_param"]
.. Again some content here
[shortcode c="c_param"]
Tengo un shortcode que toma 3 o más parámetros. Quiero saber cuántas veces se usa el shortcode en el contenido y sus parámetros en una matriz como,
array (
[0] => array(a => a_param, b=> null, c=>null),
[1] => array(a => null, b=> b_param, c=>null),
[2] => array(a => null, b=> null, c=>c_param),
)
Necesito hacer esto en el filtro the_content, filtro wp_head o algo similar.
Cómo puedo hacer esto ?
Gracias,
Tamil Selvan C
en wordpress get_shortcode_regex() La función devuelve la expresión regular utilizada para buscar códigos cortos dentro de las publicaciones.
$pattern = get_shortcode_regex();
Luego preg_match el patrón con el contenido de la publicación
if ( preg_match_all( "https://stackoverflow.com/". $pattern .'/s', $post->post_content, $matches ) )
Si devuelve verdadero, los detalles del shortcode extraído se guardan en la variable $matches.
Probar
global $post;
$result = array();
//get shortcode regex pattern wordpress function
$pattern = get_shortcode_regex();
if ( preg_match_all( "https://stackoverflow.com/". $pattern .'/s', $post->post_content, $matches ) )
{
$keys = array();
$result = array();
foreach( $matches[0] as $key => $value) {
// $matches[3] return the shortcode attribute as string
// replace space with '&' for parse_str() function
$get = str_replace(" ", "&" , $matches[3][$key] );
parse_str($get, $output);
//get all shortcode attribute keys
$keys = array_unique( array_merge( $keys, array_keys($output)) );
$result[] = $output;
}
//var_dump($result);
if( $keys && $result ) {
// Loop the result array and add the missing shortcode attribute key
foreach ($result as $key => $value) {
// Loop the shortcode attribute key
foreach ($keys as $attr_key) {
$result[$key][$attr_key] = isset( $result[$key][$attr_key] ) ? $result[$key][$attr_key] : NULL;
}
//sort the array key
ksort( $result[$key]);
}
}
//display the result
print_r($result);
}
-
Esto no funcionará si hay espacios en el valor del atributo porque str_replace() también los afecta.
– Chris J. Zähller
12 dic 2019 a las 22:24
-
@ ChrisJ.Zähller Gracias por señalar mi error. Lo corregiré pronto y actualizaré la respuesta.
– Tamil Selvan C
5 de febrero de 2020 a las 2:19
Solo para ahorrar tiempo, esta es la función que hice en base a la respuesta de @Tamil Selvan C:
function get_shortcode_attributes( $shortcode_tag ) {
global $post;
if( has_shortcode( $post->post_content, $shortcode_tag ) ) {
$output = array();
//get shortcode regex pattern wordpress function
$pattern = get_shortcode_regex( [ $shortcode_tag ] );
if ( preg_match_all( "https://stackoverflow.com/". $pattern .'/s', $post->post_content, $matches ) )
{
$keys = array();
$output = array();
foreach( $matches[0] as $key => $value) {
// $matches[3] return the shortcode attribute as string
// replace space with '&' for parse_str() function
$get = str_replace(" ", "&" , trim( $matches[3][$key] ) );
$get = str_replace('"', '' , $get );
parse_str( $get, $sub_output );
//get all shortcode attribute keys
$keys = array_unique( array_merge( $keys, array_keys( $sub_output )) );
$output[] = $sub_output;
}
if( $keys && $output ) {
// Loop the output array and add the missing shortcode attribute key
foreach ($output as $key => $value) {
// Loop the shortcode attribute key
foreach ($keys as $attr_key) {
$output[$key][$attr_key] = isset( $output[$key] ) && isset( $output[$key] ) ? $output[$key][$attr_key] : NULL;
}
//sort the array key
ksort( $output[$key]);
}
}
}
return $output;
}else{
return false;
}
}
posible duplicado del código abreviado de WordPress número de veces utilizado en la publicación
– rnevio
11 de septiembre de 2015 a las 12:52