Disabling HTML on Comments - wordpress

I've been researching for the past few hours, trying to find a way to disable HTML in WordPress comments. So far this one consistently appeared on top of Google search results numerous times:
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
This code did not work with the latest version of WordPress. I also found many more codes that again, did not work. So how would one go about disabling HTML in WordPress 3.6 (the latest version) comments?

This removed the ability for users to post HTML (but not links for some strange reason) within comments:
add_filter( 'pre_comment_content', 'wp_specialchars' );
This removed the ability for users to post links within comments:
remove_filter('comment_text', 'make_clickable', 9);

To disable HTML tags in comments, put the following code into your theme's functions.php:
add_filter('comment_text', 'wp_filter_nohtml_kses');
add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
add_filter('comment_excerpt', 'wp_filter_nohtml_kses');

Related

Can I display a custom field inside a Loop Query block?

I’m fairly new to Wordpress and I’m trying all my best to learn, but I can’t find a solution for this one…
I’ve been using the Twenty Twenty-Two theme and I’ve added a custom field ("city") to my articles.
Of course when I add a Query Loop block on my homepage, the visual editor allows me to use Titles, Categories and so on… but it won’t let me use my custom field.
I tried adding what follows to functions.php:
add_shortcode( 'city', 'return_my_custom_value' );
function return_my_custom_value() {
$myCity = do_shortcode(get_post_meta(get_the_ID(), 'city', true));
return $myCity;
}
But it won’t work, because then I use the following shortcode inside my Query Loop block:
[city]
and it shows a list of my articles all with the same 'city' value, repeated — which is the one I assigned to the page I’m showing the query on (my homepage).
Any solution for this? I would really appreciate each suggestion. Thank you so much in advance!
We have checked the code you are not calling shortcode inside the query loop. we have updated the code you can use the code. Please check below the updated code and add in the functions.php file.
add_shortcode( 'city', 'return_my_custom_value' );
function return_my_custom_value() {
$myCity = get_post_meta(get_the_ID(), 'city', true);
return $myCity;
}
Please use the following shortcode inside your query loop block
echo do_shortcode('[city]');

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Automatically add a group of tags to NEW wordpress posts

I have a blog where I make alot of posts every day. Many of the posts use 5 of 10 commonly used tags. Instead of writing out each of these tags every time, I'd rather just uncheck a few of the tags I don't need (and then add any new, or uncommon tags that I have).
So, I'm looking to edit my functions.php document so that any NEW posts will already have a list of 10 tags in it. And, if tags already exists, don't do anything. I'd like to avoid using a plugin, if possible.
Anyone know how to do this? This would be extremely useful.
The code would look something like:
(note: my programming skills are horrendous, so this may not be right, or even possible)
function default_tag_list() {
if(new_post() && !get_the_tag_list()) { // using new_post() ... not sure how to check if its a new post.
$default_tags = array('health', 'nutrition', 'diet', 'well-being', 'eating');
return $default_tags;
} // Then, somehow get the get_tag_list in the administration to use the default tags function (if it's a new post without any tags) ...
}
}
Just copy and paste the below code in your function.php :
function my_data_update () {
GLOBAL $post;
wp_set_post_tags( $post->ID, 'health, nutrition, diet, well-being, eating', true );
}
add_action('publish_post', 'my_data_update');
add_action('save_post', 'my_data_update');
this code will definitely work for you as it is tested.

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.

Inserting Wordpress Plugin content to posts

I am trying to learn more about Wordpress and creating plugins. I have seen an existing plugin use a technique where you can add a 'reference' to it within your posts and WP will parse it and replace it with the plugins own content. The example i am referring to is the NextGen gallery which uses the following code
[nextgen id=9]
I have tried searching for how this technique works but trying to find something that you don't know the name of is pretty difficult!
Can anyone point me towards some resources about how to use this feature of WP?
The technique is called shortcodes.
add_shortcode('my-content','my_plugin_shortcode');
function my_plugin_shortcode($atts, $content = null) {
$atts = shortcode_atts($my_default_atts,$atts); // $atts is now an associate array
$my_content = 'This is some content.';
return $my_content;
}
Then, if you have a post with the following content:
Hey, here is some content.
[my-content]
You will get the following output when the post is displayed:
Hey, here is some content. This is
some content.
If you passed a shortcode like [my-content id="9" test="test"], then the $atts variable in the above function would be like the following array declaration
$atts = array('id'=>9, 'test'=>'test');
The $content variable only has content when you use matching shortcodes around some text:
[my-content]This is some test
content.[my-content]

Resources