• 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

Viewing 11 replies - 16 through 26 (of 26 total)
  • Thread Starter Mr.Harmlos

    (@mrharmlos)

    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 …

    hmm in that case have a look at the function wp_set_object_terms

    it’s in wp-includes/taxonomy.php

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    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(

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    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 …

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    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 ….

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    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 …

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    To remember: autosaved drafts get a $_POST[‘temp_ID’] …

    hey what is ‘_Post’ …. ah okay – http://codex.wordpress.org/Function_Reference/stripslashes_deep

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    $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 …

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    array( ‘taxonomy’ => $tax_name ) ( $taxonomy = ‘post_tag’ )

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

    Thread Starter Mr.Harmlos

    (@mrharmlos)

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

    Thread Starter Mr.Harmlos

    (@mrharmlos)

    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…

Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘Big Problem – Filter / Hook – Pre DB insert’ is closed to new replies.