Wordpress remove shortcode from content - wordpress

Is it possible to remove the gallery shortcode from the content before when the_content() is executed? I searched codex and found remove_shortcode( $tag ) but they show no examples.
I tried adding to functions
function remove_gallery($content) {
$content .= remove_shortcode('[gallery]');
return $content;
}
add_filter( 'the_content', 'remove_gallery', 6);
It doesnt work..
Update:
I was able to unregister the shortcode using the code below, but it also removes the content
function remove_gallery($content) {
return remove_shortcode('gallery', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);

I know this is a relative old question, but the strip_shortcodes function does work!
global $post;
echo strip_shortcodes($post->post_content);
Easiest way if you ask me..

Strange. remove_shortcode (codex link) doesn't take a second argument.
You're returning either the true or false return of the remove_shortcode function, not the content with the shortcode removed.
Try either something like this in that second version of your function up there:
remove_shortcode('gallery');
return $content;
Or just put
remove_shortcode('gallery');
In your functions.php file. The previous poster suggested including the [ ]'s, which I guess is wrong.

I think you should use sub string replacement like this:
function remove_gallery($content) {
return str_replace('[gallery]', '', $content);
}
add_filter( 'the_content', 'remove_gallery', 6);
Bear in mind, this method does not come with good performance.
update: You can unregister the shotcode in function.php by adding code:
remove_shortcode('[gallery]');

An old question, but after some digging and a combination of answers this worked for me:
<?php $content = get_the_content();
echo strip_shortcodes($content);?>
I was only inserting galleries, which I wanted to remove and display separately. Obviously if you want to remove a specific shortcode this might not be the solution for you.

Related

Wordpress manipulate comment_reply_link() add #nav-comform to the link

I am working on a theme for a friend but get stuck ...
The comments and the comment form are inside a jquery tab.
To toggle the tab on a klick of the reply link i have to add #nav-comform to the link.
Example:
http://localhost/?p=109&replytocom=10#respond#nav-comform
I know i have to work with a filter in the functions.php but i have never done it before so i am a little lost and everything i try fail ...
I know it should be something like this filter example to add rel="nofollow" to the reply link:
function add_nofollow_to_reply_link( $link ) {
return str_replace( '")\'>', '")\' rel=\'nofollow\'>', $link );
}
add_filter( 'comment_reply_link', 'add_nofollow_to_reply_link' );
Maybe some one can lead me a way ?
Thank you very much !!
Try the following
function add_link_hash($args){
$args['respond_id'] = 'nav-comform';
return $args;
}
add_filter('comment_reply_link_args', 'add_link_hash', 10);

Remove a div with ID from the_content WordPress

I was trying to remove a div having some ID from the_content WordPress.
I am trying to achieve something like this
jQuery( "#some_id" ).remove();
but on server side,
I don't have a clue how I can do this on server side within the_content filter hook.
This is something I like to achieve,
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$content.find('#some_id').remove();
return $content;
}
I think this is the answer for your problem: https://stackoverflow.com/a/1114925/7335278
In your case this would look like:
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$doc = new DOMDocument();
$doc->loadHTML($content);
$element = $doc->getElementById('some_id');
$element->parentNode->removeChild($element);
$content = $doc->saveHTML();
return $content;
}
hth, let us know if this will work.
cheers, joel
The code is not tested. Just wrote for you... check this code... if this gives any syntactical error plz fix and let me know. But inside the content hook HTML/jsscripts are not running so you can try something like the following --
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$content = preg_replace("(<([a-z]+id=\"some_id\")>.*?</\\1>)is","",$content);
return $content;
}
Sorry for the previous answer.
I saw to late that what I answered was not what you were after.
Here something that might work.
$str = "<html><head></head><body><span id='remove_this'>This is one
text</span><br /><span>this is a text after that</span></body></html>";
$id_pos = strpos($str, 'remove_this');
$tmp_str = substr($str, ($id_pos-10), (strpos(substr($str, ($id_pos-10)), '</span>' )+7));
$str = str_replace($tmp_str, '', $str);
echo $str;
in the example above I start with searching for where the id is located in the string.
Than I do a substring function to get the start of the html element, in this case being the span.
Than in the same substring call I search for the first closing span tag and add 7 to the position to include the closing span tag.
than I replace the result of the substring in the original string and you get the result you are looking for.
I know there are always better ways but this is one :)
NOTE: This example of mine does use set amount of distance between the id position and the start of the tag as it directly after the start.
So there are most likely much better ways to get to the first instance of the < in php but I don't know it yet.
Hope this might get you a bit further.
I could also have gone into regular expressions but this is easier although all but the best way.
Regular expressions on the other hand are a lot more complex and I'm not really familiar with that stuff.
Cheers
and again sorry for the wrong answer at first.

How to deactivate past shortcodes in Wordpress

Recently I changed the theme of my site, and I found many of my articles use a shortcode like this
[box]
....
[/box]
My new theme does not support it and I actually don't need this shortcode to function. I thought I could just write a empty function for the shortcode in function.php, like this
function shortcode_box() {
return "";
}
add_shortcode('box', 'shortcode_box');
but it's not working.
Do you know any method to deactivate this short code?
So, you want to leave the [box] bits in the posts and/or pages, but have them not do anything? Try a shortcode that passes through the content unchanged:
function shortcode_box( $atts, $content = null ) {
return $content;
}
add_shortcode( 'box', 'shortcode_box' );
(For enclosing shortcodes, the return value of the function is used to replace the entire shortcode.)
Use remove_shortcode()
remove_shortcode('box');
Reference: http://codex.wordpress.org/Function_Reference/remove_shortcode

wordpress add_filer to the_content

I have the following filter to add a function to the post content:
function hook_shrinkshare( $content ){
if(is_single()) {
$content.= get_social_share();
return $content;
}}
add_filter('the_content', 'hook_shrinkshare' ,'100');
I would like the above to be added to the bottom of the original post content. For some reason it is adding it above.
Can someone point me in the right direction please?
Is the $content passed to the function null/empty?
Try this:
add_filter('the_content', 'hook_shrinkshare' ,'100', 1);
This lets the filter know that the function takes one argument.

Wordpress - Apply remove_filter only on one page

I am inserting multiple pages in one page (with showmultiplepages plugin), and one page includes a php file (with exec-php).
I want to disable a filter only for this included page. If I add
remove_filter( 'the_content', 'wpautop' );
to my included page, any page comming after this page won't have filters too.
Is there some tag like 'the_page' so that only the page will have no filter?
Thanks for help.
I know this is an old question, but I thought I'd chime in for anyone looking to do this in a more general sense (e.g. not in conjunction with plugin output) and say that you could also just add this to your functions.php file:
add_filter('the_content', 'specific_no_wpautop', 9);
function specific_no_wpautop($content) {
if (is_page('YOUR PAGE')) { // or whatever other condition you like
remove_filter( 'the_content', 'wpautop' );
return $content;
} else {
return $content;
}
}
I suggest creating a page template for the "one page includes a php file (with exec-php)". Then add an if statement around the remove_filter(...) statement.
if (!is_page_template('my-page.php'))
remove_filter('the_content', 'wpautop');
Hope it works. ;P
Like mroncetwice, I too realize that this is an old question; however, I came to this thread looking for an answer when I saw his. I decided to improve upon it (in terms of suiting my own situation) and am sharing the results with the hope that it may also help others.
Turn wpautop on or off by default and list any exceptions:
/**
* Allow or remove wpautop based on criteria
*/
function conditional_wpautop($content) {
// true = wpautop is ON unless any exceptions are met
// false = wpautop is OFF unless any exceptions are met
$wpautop_on_by_default = true;
// List exceptions here (each exception should either return true or false)
$exceptions = array(
is_page_template('page-example-template.php'),
is_page('example-page'),
);
// Checks to see if any exceptions are met // Returns true or false
$exception_is_met = in_array(true, $exceptions);
// Returns the content
if ($wpautop_on_by_default==$exception_is_met) {
remove_filter('the_content','wpautop');
return $content;
} else {
return $content;
}
}
add_filter('the_content', 'conditional_wpautop', 9);
Does not work?
add_filter('the_content', 'specific_no_wpautop', 9);
function specific_no_wpautop($content) {
global $post;
if (is_single('5136') || ('3820')){ // or whatever other condition you like
remove_filter( 'the_content', 'wpautop' );
return $content;
} else {
return $content;
}
}

Resources