Remove a div with ID from the_content WordPress - 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.

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);

Woocommerce Filter Hooks

I'm hoping to get a little insite for using filters in Woocommerce. My main question is, what am I looking for in the template files? or what are the variables that can be targeted using filters? If we look at the filters list I see filter name and files. Using this filter
single_product_small_thumbnail_size
Files - product-thumbnail.php and woocommerce-template.php
What am I looking for in those files that can be targeted and changed? Would you give me a simple example? Maybe something like change thumbnail size?
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable ) {
// Your code
return $variable;
}
I understand what each part of the function and filter are, but I'm not sure what code to write for "Your code." What variable am I grabbing from the file? How do I apply the change? I can't completely wrap my mind around this. Any help would be greatly appreciated.
Thanks,
~MK
As you can see in e.g. woocommerce-template.php you can filter the shop_catalog string:
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
That string is used in the subsequent code to determine the image size to be used:
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
So, if you would like to use another image size, you can filter the string like e.g.:
add_filter( 'single_product_small_thumbnail_size', 'my_single_product_small_thumbnail_size', 25, 1 );
function my_single_product_small_thumbnail_size( $size ) {
$size = 'large';
return $size;
}

paginate_links() outputs "paged=" to link instead of "page="

I've been having a sever and completely unsolvable problem with pagination.
I did notice however this interesting point:
I search a keyword like cute:
?s=cute&submit=Search&post_type=image&paged=2
...is where the link leads to. "Paged=" gives 404 on random pages. But if I modify the URL to say page=
?s=cute&submit=Search&post_type=image&page=2
Every page destination works, bringing joy to my heart, but the pagination tab list always sticks at 0 (not reflecting current page).
I feel like I'm playing a "shell game" with code and settings, with wordpress bamboozling me.
Question reduces to:
How can I get &page= to be output in every case? Conversely, if &paged= should be the way it goes, how do I get it to work without 404s?!?!
This is a problem I've dealt with for almost 3 months now. People are shy to answer.
update:
Trying to deal with this paged variable in the URL which breaks pagination, I created a filter to simply replace it with page.
function symbiostock_pagination_mod( $args ){
$args = str_replace('paged=', 'page=', $args);
return $args;
}
add_filter( 'paginate_links', 'symbiostock_pagination_mod', 1 );
Would seem like a great idea! But then this happens with every new click:
?s=cute&post_type=image&page=2&page=2
?s=cute&post_type=image&page=2&page=3
So where is this being modded, and why?
update:
I'm not sure if I'm the only one that has ever had this problem, but I did solve it (at least for myself) you can see the solution below. Also thanks for the help given. :D
So here is what I came up with...we might as well call it a hack or work-around because its unknown if this was indeed a bug I'm dealing with (from wordpress itself) or just a perfectly hidden problem in my theme's code.
The below function filters the 'paginate_links()' function and achieves this:
I see paged variable generates 404 randomly on search pages, but works fine on archive and taxonomy pages. So, we check that. if_search(), we do our changes...
If this is a search, we get the would-be page # destination using regex (or simply string replace paged to page, depending on permalink structure. We use remove_query_var() to drop faulty variable, and add_query_var() to put the working one.
This is being done because in this strange case, page always generates proper results.
Then we return the edited destination link, and life is good.
Function below:
add_filter( 'paginate_links', 'my_search_pagination_mod', 1 );
function my_search_pagination_mod( $link )
{
if ( is_search() ) {
$pattern = '/page\/([0-9]+)\//';
if ( preg_match( $pattern, $link, $matches ) ) {
$number = $matches[ 1 ];
$link = remove_query_arg( 'paged' );
$link = add_query_arg( 'page', $number );
} else {
$link = str_replace( 'paged', 'page', $link );
}
}
return $link;
}
Also thanks to some help I got here with the regular expression: PHP Regular Expression Help: A quick way to extract the 2 from "page/2/"

WordPress: remove #038 from shortcode

Simple example of a shortcode:
function s_print( $atts ){
return 'http://abc.com/?foo=1&bar=2';
}
add_shortcode( 'my_shortcode', 's_print' );
And it returns:
http://abc.com/?foo=1&bar=2
This function inserts a link to page's body via shortcode [my_shortcode], but & is always changed to &#038, and this breaks the link (it's not working anymore).
I googled a lot. There are some solutions:
wp_specialchars_decode($foo);
remove_filter('the_content','wptexturize');
But those seems to be only for use in theme (functions.php) and it doesn't work for a shortcode (I tried adding it before or inside the shortcode function).
I don't want to fall to last solution, which is commenting some lines in WordPress formatting.php file because I'm working on a plugin which will be used by many people.
I had a similar problem that I addressed with the clean_url filter. See the edit on my answer here.
It wasn't in a shortcode, so I can't guarantee it'll work in your particular situation. Might be worth a shot though.
EDIT by oyatek
(modified solution from the link aboove):
function so_handle_038($content) {
$content = str_replace(array("&","&"), "&", $content);
return $content;
}
add_filter('the_content', 'so_handle_038', 199, 1);
The fact is when you use the Visual Editor, the & will be changed to &. But if you use the Text Editor, single & followed by no character remains the same while &sth will be changed to &sth.
Altogether & will be changed to either & or &. I think the solution above:
function so_handle_038($content) {
$content = str_replace(array("&","&"), "&", $content);
return $content;
}
add_filter('the_content', 'so_handle_038', 199, 1);
is a sort of overkill, because so_handle_038(); decodes all &s and &s in the $content while in your case, you need to decode only those in the $atts array. Your shortcode entry is probably like this:
[my_shortcode url="http://abc.com/?foo=1&bar=2" /]
and the $atts will be:
array( 'url' => "http://abc.com/?foo=1&bar=2" )
or:
array( 'url' => "http://abc.com/?foo=1&bar=2" )
so you only need to decode $atts['url']:
html_entities_decode($atts['url']);
before you try to do anything on it.
This one was a bandaid hack which I used, basically to fix the issue in wordpress (As I had my link inside of an iFrame), I went to Bitly and created a link WITHOUT the ampersand & sign! So finally I ended up getting a link that DIDN'T have any of the & signs but STILL pointed at the same location (Plus I got free tracking via Bit.ly for the link so DOUBLE bonus as I can check how many times the link was clicked). Not the "Best" solution, but heck it worked for me and I didn't have to waste time trying to figure out some other solution for the & symbol in Wordpress.

Wordpress remove shortcode from content

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.

Resources