WordPress.org

Support

Support » Plugins and Hacks » Big Problem – Filter / Hook – Pre DB insert

Big Problem – Filter / Hook – Pre DB insert

  • 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 15 replies - 1 through 15 (of 26 total)
  • elfin
    Participant

    @elfin

    do it on display, rather than on store.

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

    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

    elfin
    Participant

    @elfin

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

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

    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 …

    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 ๐Ÿ˜‰

    elfin
    Participant

    @elfin

    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 …

    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 …

    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

    elfin
    Participant

    @elfin

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

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

    elfin
    Participant

    @elfin

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

    ill try … if it works you have my gratitude for a looong time ๐Ÿ˜‰

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