WordPress.org

Ready to get started?Download WordPress

Forums

Big Problem - Filter / Hook - Pre DB insert (27 posts)

  1. Mr.Harmlos
    Member
    Posted 3 years ago #

    Hi

    Im trying and researching for nearly a week now to figure out what seems a simple problem.

    Want: Simply autoconvert twitter hashtags to wordpress tags.

    Problem: This has to happen before or when the data is published.

    function hashtags_to_tags() {
    
    		global $post;
    		$id = (int) $post->ID;
    		$content = $post->post_content;
    
    		preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches );
    
    		//wp_set_object_terms( $id, $matches[0], 'post_tag', true );
    		wp_set_post_tags( $id, $matches[0], true );
    	}

    this works fine when the post already is created and has a ID ...

    How i understand it i have to alter the temporary array in which the postcontent is stored before it is inserted in the DB ...

    I have gone through post.php and as i understand there are no tags in the temp array and they are added somehow ???

    thats the point i dont understand.

    How are tags added when you normaly create a post and how can i add my filter/function ???

    help would be saving my mental health ...
    greets

  2. elfin
    Moderator
    Posted 3 years ago #

    do it on display, rather than on store.

    so add a filter to the content by using add_filter('the_content','your_function')

  3. Mr.Harmlos
    Member
    Posted 3 years ago #

    Thy for your care but ...

    The thing is is want to show only certain tagged Posts on the frontpage ...

    So these post wont be shown and therefore wont be tagged ... thats the problem ...

    greets

  4. elfin
    Moderator
    Posted 3 years ago #

    then use custom meta to mark which ones you want, or don't want, tagged.

  5. Mr.Harmlos
    Member
    Posted 3 years ago #

    How would that change the problem ? Havent looked into meta stuff ...

  6. Mr.Harmlos
    Member
    Posted 3 years ago #

    How would i get the content block of a post when i want to filter it with the publish post hook ?

    Thats my only problem atm ...

  7. Mr.Harmlos
    Member
    Posted 3 years ago #

    function hashtags_to_tags($post_id = 0) {
    
    		$content = get_post($post_ID); /doesnt work atm
    
    		preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches );
    		// preg_replace ( '(#)', '', $matches[0]);
    
    		wp_set_post_terms( $post_id, $matches[0], $taxonomy = 'post_tag', $append = true );
    		wp_set_object_terms( $post_id, $matches[0], $taxonomy = 'post_tag', $append = true );
    		wp_set_post_tags( $post_id, $matches[0], $taxonomy = 'post_tag', $append = true );
    
    	}
    add_action('publish_post','hashtags_to_tags');

    This works when i input manually like:

    content = '#test'

    Read to much without understanding it completely ... sorry for buggin ;)

  8. elfin
    Moderator
    Posted 3 years ago #

  9. Mr.Harmlos
    Member
    Posted 3 years ago #

    read most of that ...

    Problem is the Content isnt saved in DB ... and therefore not acessible via the easy ways ...

    function wp_insert_post($postarr = array(), $wp_error = false) {
    	global $wpdb, $wp_rewrite, $user_ID;

    I think i have to grab it from $postarr or $data ... in post.php ...

    ...

  10. Mr.Harmlos
    Member
    Posted 3 years ago #

    Doesnt work that way ...

    as i understand i have to get the post content by its ID ...

    Which seem s to be '0' in case of a new unpublished post ...
    Found nothing about that in the Codex ...

  11. Mr.Harmlos
    Member
    Posted 3 years ago #

  12. Mr.Harmlos
    Member
    Posted 3 years ago #

    global $data; $content = $data['post_content']; nope
    global $post; $content = $post['post_content']; nope
    global $postarr; $content = $postarr['post_content']; nope

    $content = wp_get_object_terms( $post_id, $taxonomy = 'post_content', $args = string); Nope

    $content = get_post($post_id, ARRAY_A); nope

    $content = wp_get_single_post($post_id, ARRAY_A); nope

  13. elfin
    Moderator
    Posted 3 years ago #

    That's because I'm still suggesting you change the output, rather than trying to change what is inserted into the database.

  14. Mr.Harmlos
    Member
    Posted 3 years ago #

    $postdata= $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$post_id"); nope

    I understand what you mean ... i tried dozends of plugins codesnippets and whatever ...

    Most people used the the_content hook in the loop or cronjobs triggerd afterwards etc ... but...

    This is crazy ... this is a one time job when the post is saved and a possible million times job when in the loop ...

  15. elfin
    Moderator
    Posted 3 years ago #

    Look at using this filter: wp_insert_post_data, it's sent 2 arrays $data & $postarr and you'll need to return the data.

  16. Mr.Harmlos
    Member
    Posted 3 years ago #

    ill try ... if it works you have my gratitude for a looong time ;)

  17. Mr.Harmlos
    Member
    Posted 3 years ago #

    I came up with

    function hashtag_to_tag( $data , $postarr )   // wp_insert_post_data
    {
    
      $content = $data['post_content'];
      preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches );
      $stringed = implode(",", $matches);
      $data['tags_input'] = $stringed;    //'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
    
      return $data;
      return $postarr;
    }
    add_filter('wp_insert_post_data', 'hashtag_to_tag', 2, 2);

    But
    'tags_input' is only in - http://codex.wordpress.org/Function_Reference/wp_insert_post

    Not in http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data

    if i save this in functions.php of my template the Publish button changes to "Submit for Review" and the post is going nowhere and cant be found ....

    Do i have to change the default array in wp_insert_post in post.php or where is the problem ???

    if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
    		wp_set_post_tags( $post_ID, $tags_input );

    this is in function wp_insert_post line:2342
    is this executed after thewp_insert_post_data ..i dont think so ..but i dont get it ...

  18. elfin
    Moderator
    Posted 3 years ago #

    hmm in that case have a look at the function wp_set_object_terms

    it's in wp-includes/taxonomy.php

  19. Mr.Harmlos
    Member
    Posted 3 years ago #

    Found solution for "Submit for Review" Problem

    add_filter( 'wp_insert_post_data', 'avoid_blog_page_permalink_collision', 10, 2 );

    in includes/ms.php line 738-759 ...

    have to set priority behind 10 so i set it to 11 ;)

    But no effect on the posted tags ... X(

  20. Mr.Harmlos
    Member
    Posted 3 years ago #

    wp_set_object_terms aka wp_set_post_terms as i posted above in combination with $post_id was the only thing that worked as i wanted ...

    But i cant get the content the same way ...

  21. Mr.Harmlos
    Member
    Posted 3 years ago #

    Im done for today ...

    This is still the best shot:

    function hashtags_to_tags($post_id = 0) {
    
    		$content = get_post($post_ID); /doesnt work atm
    
    		preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches );
    		// preg_replace ( '(#)', '', $matches[0]);
    
    		wp_set_post_tags( $post_id, $matches[0], $taxonomy = 'post_tag', $append = true );
    
    	}
    add_action('publish_post','hashtags_to_tags');

    It adds the tags as i want but i cant get any content input in the function ...

    I think this isnt possible ... will have to find another solution

    the 3rd time i try a new way ... frustrating ...

    Want: Simply autoconvert twitter hashtags to wordpress tags.

    Is wordpress not compatible with this simple small wish ...

    if i count the hours ... i would be a week i´m sitting on this single problem.

    Not cool - Im done for today ....

  22. Mr.Harmlos
    Member
    Posted 3 years ago #

    function hashtags_to_tags($post_id) {
    		global $post;
    		$content = $post->post_content; // get the content of post
    		preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches ); // filter hashtags
    		$out = (array_values($matches)); // cleanup array [expletive deleted] ugly but works
    		wp_set_post_terms( $post_id, $tags = $out[0], $taxonomy = 'post_tag', $append = true );
    	}

    This works when published und updated afterwards ...

    Still not when just published ...

    I crawled through wordpress sourcecode and looked in the mysqldb ...
    I know now that the 'tags' are just linked with the posts and not saved in the same dataset ...

    But i still dont get it why this doesnt work like i want ...
    I mean when you make a post manually you can add taggs but not when imported automatically from rss or whatever input ...

    Help is appreciated ...

  23. Mr.Harmlos
    Member
    Posted 3 years ago #

    To remember: autosaved drafts get a $_POST['temp_ID'] ...

    hey what is '_Post' .... ah okay - http://codex.wordpress.org/Function_Reference/stripslashes_deep

  24. Mr.Harmlos
    Member
    Posted 3 years ago #

    $label = $taxonomy->labels->name;
    
    	if ( !is_taxonomy_hierarchical($tax_name) )
    		add_meta_box('tagsdiv-' . $tax_name, $label, 'post_tags_meta_box', $post_type, 'side', 'core', array( 'taxonomy' => $tax_name ));
    	else
    		add_meta_box($tax_name . 'div', $label, 'post_categories_meta_box', $post_type, 'side', 'core', array( 'taxonomy' => $tax_name ));
    do_action('add_meta_boxes', $post_type, $post);
    do_action('add_meta_boxes_' . $post_type, $post);
    
    do_action('do_meta_boxes', $post_type, 'normal', $post);
    do_action('do_meta_boxes', $post_type, 'advanced', $post);
    do_action('do_meta_boxes', $post_type, 'side', $post);

    File: Edit-form-advanced.php

    As i understand this is the code used to get the tags in the editor linked to the post ...

    So the Tags are actually meta_boxes ...

  25. Mr.Harmlos
    Member
    Posted 3 years ago #

    array( 'taxonomy' => $tax_name ) ( $taxonomy = 'post_tag' )

    This is where i have to write the tags in context with the Post id

    ...

  26. Mr.Harmlos
    Member
    Posted 3 years ago #

    I break together .... I think i got it now ... with the almighty help of the worsepress commmunity ...

  27. Mr.Harmlos
    Member
    Posted 3 years ago #

    So F U C K yeah ::: dONE

    function hashtags_to_tags($post_id, $post) { 															// both are needed !
    
    		$content = $post->post_content; 																// get the content of post
    
    		preg_match_all( '/\B#(\w*[a-zA-Z-]+\w*)/', $content, $matches ); 								// filter hashtags
    		$out = (array_values($matches)); 																// cleanup array fucking ugly but works
    
    		wp_set_object_terms( $post_id, $tags = $out[0], $taxonomy = 'post_tag', $append = true ); 		// Universal for anny input
    }
    
    add_filter('save_post','hashtags_to_tags',2, 2);   									// High priority might not needed but the 2nd parameter is

    This is an Universal Filter. Every post content is filterd AUTOMATICALLY.

    it would be awesome if somebody with more Skills could make this a Plugin like :

    http://wordpress.org/extend/plugins/wordpress-filter/

    Which isnt working automatically ... and its old and should be updated.
    Had some Bug´s wit 3.04 ...

    Thx @Rich 'elfin' Pedley ... he tried to help ...

    Anyway im DONE ... my problem is solved by me ... So i´m the only problem that is left ^^

    ...off...

Topic Closed

This topic has been closed to new replies.

About this Topic