Wordpress Gallery Shortcode Pregreplace - wordpress

Basically I need to remove the gallery shortcode from the Wordpress content, I'm using
echo preg_replace('/\[gallery ids=[^\]]+\]/', '', get_the_content() );
It is removing the gallery shortcode successfully, but also the paragraph tags which I need to keep. The idea is that I want to output everything in the content except the gallery.

You could use Wordpress strip_shortcode function.
Look at the example in the Codex.
You can create a filter that strips shortcodes:
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}
and call it when you need (in your template):
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')
EDIT 1
Another way to get that (and answering you comment) you can use Wordpress apply_filters function in the content after remove the undesirables shortcodes.
//within loop
$content = get_the_content();
$content = preg_replace('/\[gallery ids=[^\]]+\]/', '', $content );
$content = apply_filters('the_content', $content );
echo $content;
But I would not recommend to you to do that. I think forcing your site to modify the content of a post could make that hard to understanding. Maybe you should work with Wordpress Excerpt and avoid any problem.
A link that helped me

to remove a shortcode or a particular list of shortcode you can use this code.
global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* #global int $remove_shortcode
* #param type $shortcodes comma seprated string, array of shortcodes
* #return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;
if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;
$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
dot1_strip_shortcode( 'gallery' );
Accepts single, comma seprated shortcode string or array of shortcodes.

Related

WordPress ACF how to filter a textarea field content

I'm building a WordPress theme that uses the Advanced Custom Fields (ACF plugin). I have the following function via functions.php:
function filter_p_tags( $content ) {
$content = str_replace( '<p>','<p class="custom__class">', $content );
return $content;
}
add_filter('the_content', 'filter_p_tags');
add_filter('acf_the_content','filter_p_tags');
The <p> tags via posts and pages are successfully being replaced with <p class="custom__class">. However, my ACF fields are not being filtered. What am I doing wrong here?
It's worth mentioning that the ACF fields in question belong to an options page. Here's how an option field looks within one of my templates.
<?php the_field( 'text', 'option' ); ?>
If your ACF field is a textarea, then you would want to use acf/format_value/type=textarea filter hook instead of using acf_the_content which would be applied on wysiwyg.
add_filter('acf/format_value/type=textarea', 'filter_p_tags_acf', 10, 3);
So your entire code would be something like this:
add_filter('the_content', 'filter_p_tags');
function filter_p_tags( $content ) {
$content = str_replace( '<p>','<p class="custom__class">', $content );
return $content;
}
add_filter('acf/format_value/type=textarea', 'filter_p_tags_acf', 10, 3);
function filter_p_tags_acf( $value, $post_id, $field ) {
$value = str_replace( '<p>','<p class="custom__class">', $value );
return $value;
}
Another way of doing this
Alternatively, as you suggested, we could use acf/format_value/key={$key} filter hook instead of using acf/format_value/type=textarea. Like so:
add_filter('acf/format_value/key=field_abc123456', 'filter_p_tags_acf', 10, 3);
function filter_p_tags_acf( $value, $post_id, $field ) {
$value = str_replace( '<p>','<p class="custom__class">', $value );
return $value;
}

How to echo something after header tag (wordpress)?

I want to echo something after header tag in single pages.
I have a filter to add something before content.
function add_custom_meta_to_content($content) {
$queried_object = get_queried_object();
if ( $queried_object ) {
$post_id = $queried_object->ID;
}
$text = get_post_meta($post_id, 'textbox_wporg_meta_key', true);
if( is_single() ) {
$content = $text . '' . $content;
}
return $content;
}
add_filter( 'the_content','add_custom_meta_to_content' );
But Now I want add some data exactly after header tag in single pages by my plugin.
I don't want to edit theme codes.
How can I do that?
You wouldn't normally do this with the the_content filter. Almost all commercial themes would have a specific hook to insert additional code after the post title and before the main content. For instance, the Generatepress framework would enable you to hook into the generate_after_entry_title action hook and you would output additional code there.

Custom Page Title Shortcode within another Shortcode

I use this function to obtain the title of post page in the content area:
function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );
So, I have a plugin that when I use this shortcode below it read the text beetween the shortcode:
[responsivevoice voice="UK English Male"] Wonderful World [/responsivevoice]
So, what I'm doing and it's my question also is how can I put the "page title shortcode" within responsivevoice shortcode like this?
[responsivevoice voice="UK English Male"] [page_title] [/responsivevoice]
(It should get the title of the post page, but it doesn't work).
Choose one of the following options, and add the code snippet after:
add_shortcode( 'page_title', 'myshortcode_title' );
Option #1: [responsivevoice2]
add_shortcode( 'responsivevoice2', 'responsivevoice2' );
function responsivevoice2( $atts = array(), $content = '' ) {
if ( $content = do_shortcode( $content ) ) {
return RV_add_bblisten( $atts, $content );
}
return '';
}
Sample usage:
[responsivevoice2 voice="UK English Male"] [page_title] [/responsivevoice2]
This Shortcode allows you to use the [page_title] or any other Shortcodes in the
text to be spoken.
Option #2: [responsivevoice_page_title]
This Shortcode will always use the page title (or the title of the current post)
as the text to be spoken. With this Shortcode, you don't need the [page_title]
Shortcode anymore.
add_shortcode( 'responsivevoice_page_title', 'responsivevoice_page_title' );
function responsivevoice_page_title( $atts = array() ) {
if ( $page_title = get_the_title() ) {
return RV_add_bblisten( $atts, $page_title );
}
return '';
}
Sample usage:
[responsivevoice_page_title voice="UK English Male" /]

Use postmeta in wordpress shortcode

Trying to get my post meta from posts using shrotcodes and then displaying it on the content.This is the code that's trying to do this:
$string = '';
$custom_content = get_post_custom($post->ID);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$content = get_the_content();
$bonus = $custom_content["bonus"];
$string .= $content . $bonus . '<br>';
endwhile;
}
return $string;
It's not working as the custom content returns empty. Whats wrong? Thanks in advance
I don't think you got the shortcode idea, you should read some info about add_shortcode() also you can use get_post_meta() to retriev the metadata from the database.
Here is an example on how you can achieve this but this will only work with the main loop (as default):
<?php
//you can put this code in functions.php or you can build a plugin for it
function metadata_in_content($attr) {
//this is the function that will be triggerd when the code finds the proper shortcode in the content; $attr is the parameter passed throw the shortcode (leave null for now)
global $wpdb, $wp_query;
//we need global $wpdb to query the database and to get the curent post info
if (is_object($wp_query->post)) {
$post_id = $wp_query->post->post_id;// here we save the post id
$metadata = get_post_meta( $post_id, $key, $single ); // here we get the needed meta, make sure you place the correct $key here, also if you don't want to get an array as response pass $single as "true"
return $metadata; // this finally replaces the shortcode with it's value
}
}
add_shortcode('insert_metadata', 'metadata_in_content');
//the above code hooks the metadata_in_content function to the [insert_metadata] shortcode
?>
Now all it's left to do is to place [insert_metadata] in the post content and things should work.

Change the page title using a short code

I am coding a plugin and I use a short code to render a page content. I want to change the page title. I used this:
// Change the title of the page of the charts to add the current month.
add_filter('the_title', 'charts_title', 10, 2);
function charts_title($title, $id) {
$title .= ' ' . date('F Y');
return $title;
}
But it does that for all the posts and pages. Can I do that only for the pages that contains the short code I created ? I tried to do that, but it doesn't work.
add_shortcode('charts-vote', 'charts_vote');
function charts_vote($atts) {
// Add the filter only in the short code function callback.
add_filter('the_title', 'charts_title', 10, 2);
// ... //
return $content;
}
Can someone please help me ?
I understand that the specifics of your set up may require checking for the Shortcode, but maybe a Custom Field could be used for that:
add_filter( 'the_title', 'charts_title_so_15312385', 10, 2 );
function charts_title_so_15312385( $title, $post_id )
{
// Admin area, bail out
if( is_admin() )
return $title;
// Custom field not set, bail out
$mod_title = get_post_meta( $post_id, 'mod_title', true );
if( !$mod_title )
return $title;
// Ok, modify title
$title .= ' ' . date( 'F Y' );
return $title;
}
The Shortcode could even "talk" with the Custom Field for extended configurations.
For ease of use, you could make a Custom Meta Box or use a plugin like Advanced Custom Fields.

Resources