Sanitize inline links in wordpress? - wordpress

Hy.
So would anybody know how to write a function that would sanitize all links in a specific class that are entered through WYSISWYG editor?
I know about the wordpress in-built sanitize_title function, but I don't know how I could refer to those links(links in a specific class).
Any help much appreciated.

Why don't you use a filter function to filter the_content()?
In your functions.php file, include the filter hook:
add_filter ( 'the_content', 'your_sanitizer_function');
Then also in functions.php write the sanitizing function that will filter the normal output of the_content():
function your_sanitizer_function($content){
/* use some php here to change your content as you see fit...
for example, $content = str_replace('foo', 'bar', $content);
*/
return $content;
}

Related

is wrong to add my code in wp admin page wordpress

I must to add new options and functions in post pages in admin panel. I call a new function in edit-form-advanced.php and edded this function in template.php file. The question is this wrong? Becouse my function is in one file with functions on wordpress. Or maybe must be in other file? but where i must call it?
For wp-content part i know and i make a child theme of parent theme, but i do not know what to do when i must add code in wp-admin part.
example:
edit-form-advanced.php
do_custom_boxes( null, $post );
and in template.php
function do_custom_boxes( $screen, $object ) {
global $wpdb;
$appTable = $wpdb->prefix . "post_panel";
$query = $wpdb->prepare("SELECT * FROM $appTable WHERE post_id = ".$_GET['post']." ", $screen);
$applications = $wpdb->get_results($query);
......
}
Short answer: Yes, it's wrong to do so. Whenever you update your WordPress you'll loose all your changes.
WordPress allows you to hook into its code, modify its behavior and many things.
Please read about actions and filters.
Basically, Actions allow you to fire a function when something happens in WordPress.
For example:
<?php
function do_something_when_admin_pages_init() {
// Do something here
}
add_action('admin_init', 'do_something_when_admin_pages_init')
Filters allow you to modify data/output of another function. It's like it let you step in the middle, do something with the data and then continue.
Example from the WordPress page:
<?php
function wporg_filter_title($title) {
return 'The ' . $title . ' was filtered';
}
add_filter('the_title', 'wporg_filter_title');
This modifies the title before it's printed.
So with those two ways of 'hooking' into the WordPress code, you can write your code in your theme's functions.php file, or write a Plugin (it's up to you).

WP load scripts if template has shortcode

I have a plugin, it has many css theme files. Of course I do not want to load all of them, only one. It depends on config. For post I use has_shortcode function, but how todo the same thing with template that use do_shortcode function.
Note:
I found a good solution, I use
$this->loader->add_action( 'init', $plugin_public, 'register_scripts');
$this->loader->add_action( 'wp_footer', $plugin_public, 'print_scripts');
Inside shortcode handle I set a global var to true
global $imagelink_plugin_shortcode_used;
$imagelink_plugin_shortcode_used = true;
The function print_scripts add my scripts if my global var is true
public function print_scripts() {
global $imagelink_plugin_shortcode_used;
if ( ! $imagelink_plugin_shortcode_used )
return;
wp_print_scripts($this->plugin_name . '-imagelinks');
}
Thanks for answers.
Instead of using has_shortcode function, what you can do is register and enqueue those files when that shortcode is rendered.
First, register your css files with wp_register_script. Make sure to hook this into wp_enqueue_scripts
Now, inside your shortcode function, enqueue the files.
wp_enqueue_style('theme-css')
Using this way you can use the shortcode anywhere you want and the script is loaded only when shortcode is present on the page whether it is on content or template.

Wordpress Hook the_content Within Widget

I am writing a widget that list the headings in the post and then created hash links and edits the HTML to reflect that. I've got the list widget content figured out and I just need to edit the_content, i've tried to add a filter for the method that returns the updated code but it's not working.
What would be the best way to do this? My class is called post_headings_widget and the edited HTML content is stored within $this->the_content.
I was hoping I could do this within the widget class
public
function edited_content() {
return $this->the_content;
}
and then to edit the content output here
add_filter( 'the_content', [ 'post_headings_widget', 'edited_content' ] );
It calls the class method fine but i'm not sure exactly how it works so i'm guessing it called the method directly without calling the constructors etc?
I have also tried to just create a filter from within the widget() method but that did not work either, heres what I tried:
add_filter( 'the_content', function() {
return 'test';
} );
Any ideas on a solution?
You have to pass the_content as a parameter in your filter function/callback.
Check the Wordpress docs: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
On widgets you need to bind on widget_text
add_filter('widget_text', 'se24265_my_function');
function se24265_my_function( $content )
{
# replace code here on widget $content
return $content;
}

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

How can I use add_filter for a specific page template?

In Wordpress I have a page template called designers.php.
When loading, it reads the slug to get a uniqe ID, then calls the DB to retrieve designer information.
I want to use this information to alter the page title, using the designer name in the title tag.
I've tried using the add_filter in my designers.php file, but it's not working:
add_filter('wp_title', 'set_page_title');
function set_page_title($title) {
global $brand;
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
}
I'm, guessing the add_filter must either be located inside a plugin or in functions.php file.
How can I achieve what I'm trying to do?
UPDATE
The function is never fired as long as I use wp_title. If I change it to init (for testing), the function is fired.
So why does the add_filternor work for wp_title?
You are almost right. The filter must reside in function.php, and is called to modify the title. You can add the filter conditionally. Use this function is_page_template() to determine if wordpress is rendering your template
Try to modify your function like this:
add_filter('wp_title', 'set_page_title');
function set_page_title($title) {
global $brand;
if (is_page_template('designer.php'))
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
else
return $title;
}
First of all add_filter must either be located inside a plugin or in functions.php file.
Then, maybe you have to set the priority for the filter :
add_filter('wp_title', 'set_page_title',1);
function set_page_title() {
global $brand;
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
}
And check the <title> meta in your header.php theme.
<title><?php wp_title(); ?></title>

Resources