How to change text in editor of wordpress cms - wordpress

I want to edit the text before publication but with one condition:
the text in my editor also be edited.
A simple example:
function edit_content($the_content){
return $the_content.'ali';
}
The problem with this code: it properly read the edited text but it does not change the text in the editor.
How can I edit the text in the editor?

Use Filter Hook content_save_pre
If you want to change the content just before it saved to database, you can use this filter hook:
function my_filter_function_name( $content ) {
// do something with $content
return $content;
}
add_filter( 'content_save_pre', 'my_filter_function_name', 10, 1 );

Related

How to save textarea content as a post title

I have a textarea instead of an input field in edit post screen, and i want to save textarea content as a post_title, but i dont know how to extract the data. How?
You can use wp_insert_post_data() filter for changing post_title dynamically.
reference :- https://developer.wordpress.org/reference/hooks/wp_insert_post_data/
Try Out this code :-
replace textarea_field with your textarea name.
function zillion_filter_post_title( $data, $postarr, $unsanitized_postarr){
$data['post_title'] = $_POST['textarea_field'];
return $data;
}
add_filter( 'wp_insert_post_data', 'zillion_filter_post_title',10,3);

Change product cross sells h2 heading "You may be interested in..." on WooCommerce cart page

There is a WooCommerce Hook called woocommerce_cart_collaterals that displays a text in a div that I would like to change.
This is the short form of the unmodified HTML output:
<div class="cart-collaterals">
<div class="cross-sells">
<h2>You might be interested in...</h2>
...
</div>
</div>
I would like to change the text inside the h2. I have added an action for this hook but the given argument variable is just empty, I don't know what to do to get access to the h2.
This is what I've tried by adding to the theme's functions.php:
function action_woocommerce_cart_collaterals( $woocommerce_cart_totals) {
print_r($woocommerce_cart_totals);
};
add_action( 'woocommerce_cart_collaterals', 'action_woocommerce_cart_collaterals', 10, 1);
What to do I need to modify to edit the text output in the h2?
The woocommerce_cart_collaterals action hook callback function, contains no arguments. Use the woocommerce_product_cross_sells_products_heading filter hook instead
So you get:
function filter_woocommerce_product_cross_sells_products_heading( $string ) {
// New text
$string = __( 'My new text', 'woocommerce' );
return $string;
}
add_filter( 'woocommerce_product_cross_sells_products_heading', 'filter_woocommerce_product_cross_sells_products_heading', 10, 1 );

the_editor_content filter change the content of other textarea in wp admin in add/edit post section?

I have more then one text areas in wp admin in post add/edit section, i am trying to change the content of by default textarea of wp but when i execute the the_editor_content filter, it change the content of by default textarea but it also change the content of other textareas,is there any way to to change the content of only by default textarea?
Note* Other textareas have different ids
code i used :
add_filter( 'the_editor_content', 'my_editor_content' );
function my_editor_content() {
global $post;
return search_keywords($post->post_content, $keyword1,$keyword2,$keyword3);
}
I think you need to hook into default_content like this
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "This is my default content!";
return $content;
}

Wordpress widget for post specific content in sidebar

I am looking for a wordpress plugin that will allow me to add a paragraph to the sidebar that is specific to the blog post. I would need to be able to add that text when creating the post. Is there something out there like that? I have been unsuccessful in searches.
Thanks
This can be easily solved using Custom Fields, the Text Widget and a Shortcode.
This bit of code goes in the theme's functions.php or, preferable, within a custom plugin.
1) Enable shortcodes for the Text Widget
add_filter( 'widget_text', 'do_shortcode' );
2) Define the shortcode, read comments for details
add_shortcode( 'mytext', 'so_13735174_custom_text_widget' );
function so_13735174_custom_text_widget( $atts, $content = null )
{
global $post;
// $post contains lots of info
// Using $post->ID many more can be retrieved
// Here, we are getting the Custom Field named "text"
$html = get_post_meta( $post->ID, 'text', true );
// Custom Field not set or empty, print nothing
if( !isset( $html ) || '' == $html )
return '';
// Print Custom Field
return $html;
}
3) Add a Text Widget in the desired sidebar.
Leave the title empty and put the Shortcode in the content: [mytext].
4) Now each page or post with a Custom Field named text will have its value printed in the Widget.
5) The $html can get fancy and multiple Custom Fields can be used.
This isn't something that I've ever personally done, but try this.
Summary: You will add the a paragraph using a custom field, then display it in a widget.
Details:
First, make sure custom fields are enabled. Edit a post, then click
the "screen options" at the top right of the page. If "Custom
Fields" isn't checked, check it. You should now see a custom field
area below the post editor.
Come up with a name for your custom field. Perhaps
"extra_paragraph". Now put that in the "name" field in the custom
field area.
Write your paragraph in the "value" field the custom field area.
Install the Custom Field Widget plugin, set it to display this
new "extra_paragraph" field. (widget appears to be untested with newer versions of Wordpress so cross your fingers!)
Now when you write or edit posts you should see this "extra_paragraph" field as an option in the "name" dropdown.

Adding custom tags in Wordpress

I'm creating a new WP theme and I would like to allow the user to insert a divider in between paragraphs or images he/she is entering, for a post/page.
I want the output to be something like:
<div class="divider"></div>
But I don't want the user to have to enter HTML in the WYSIWYG editor. Is it possible to ask them to enter something like:
<-- break -->
and then translate that to the div markup on display?
Thanks.
Build a function in your theme's functions.php file like this:
function add_div( $content ) {
$content = str_replace( '<!-- break -->', '<div class="divider"></div>', $content );
return $content;
}
then add the following to the theme:
add_filter( "the_content", "add_div" );
The function uses PHP's string replace function to find the text you want your users to input and replace it with the text you want to render, the add_filter() function uses Wordpress's content filter to apply your function to the content of each post after it is read from the database, but before it is rendered to the browser.
This will work in PHP4 and up, which is still the official level of support for Wordpress.

Resources