How to save textarea content as a post title - wordpress

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

Related

How to change text in editor of wordpress cms

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

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

woocommerce add extra div after price in content-product.php

guys:
I want to add a extra div after price hook. Below is my code, but it is not working. Can anyone please tell me what I did wrong?
function init_actions() {
add_action( 'woocommerce_after_shop_loop_item_title', 'see_details_button', 10 );
}
function see_details_button() {
$button = '<div>See Details</div>';
return $button;
}
You are only returning the string you want to output. Because this is an action and not a filter I think that you should print/echo the HTML instead, i.e:
echo '<div>See Details</div>';
If that does not work. Make sure that the action actually run at the right place and that the action name is correct.

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.

Resources