• Hello,

    I have a plugin which creates automatically a custom field called ‘ytthumb_url’ when I publish a post from the backend of WordPress.

    But when I publish content from frontend, via plugins like TDO Mini Forms, Post From Site, or others : the custom field is not created, I have to edit the post from the back-office to generate automatically the custom field.

    Here is my plugin:

    <?php
    add_action('save_post','find_ytid', 10, 2);
    
    function find_ytid($postID, $post) {
    	if($parent_id = wp_is_post_revision($postID))
    		{
    		$postID = $parent_id;
    		}
    		$content = $_POST['content'];
    		if (preg_match('/http:\/\/www.youtube\.com\/v\/([a-zA-Z0-9\-\_]{11})/', $content, $yturl) !='') {
    		$ytid = substr($yturl[0], 25, 31);
    		$custom = 'http://img.youtube.com/vi/'.$ytid.'/hqdefault.jpg';
    		update_custom_meta($postID, $custom ,'ytthumb_url');
    		}
    }
    
    function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
    }
    ?>

    Anybody may help me ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’m interested, did you have try with a hidden custom field with default value in TDO form creator?
    exist an option “Treat default value as code (php)” but I have not try it.

    let me know

    Thread Starter evo252

    (@evo252)

    Hello Torg,

    I don’t find any option called “hidden custom field” in TDO Mini Forms plugin, may you help me to find it ? Do you talk about “append to post content” widget ? I’ve tried to put the plugin in this box, but it doesn’t run :/

    Any solutions ?

    it’s a Type, like “text field” “text area” “checkbox” and “list”

    Type = Hidden Field
    Default Value = Yes

    but if the data is confidential (in my case would be the day on which the post expire and the user should be not able to change it)
    Note that if the visitor has Firebug (or similar) can eliminate the type “hidden field” and he can change the default value!!!

    Thread Starter evo252

    (@evo252)

    OK I found it, but what can I tape on “Default Value” ? I want my plugin scan the post to found some youtube video, and I don’t know how to link this function with TDO mini forms via “hidden field”…!

    I’ve tried with “One Quick Post” plugin, it doesn’t run too… Why all these plugins don’t run with my plugin, whereas from backoffice, the plugin runs perflectly ?

    ok, I have set my registration form to add a “user expire date” from 6 months after the registration date.

    //Example to add 6 months to a date object
    $currentDate = date("Y-m-d");// current date
    //Add 6 months to current date
    $dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +6 month");
    echo date('Y-m-d', $dateOneYearAdded);
    $expiredate = date('Y-m-d', $dateOneYearAdded);
    ?>
    	<input id="expire097" type="hidden" tabindex="30" size="25" value="<?php echo $expiredate; ?>" name="expire097" />
    <?php
    }
    
    function check_fields ( $login, $email, $errors )
    {
    	global $expire097;
    	if ( $_POST['expire097'] != $expiredate )
    	{
    		$errors->add( 'empty_realname', "<strong>ERROR</strong>: Please don't hack website" );
    	}
    	else
    	{
    		$expire097 = $_POST['expire097'];
    	}
    }
    function register_extra_fields ( $user_id, $password = "", $meta = array() )
    {
    	update_user_meta( $user_id, 'expire097', $_POST['expire097'] );
    }
    ?>

    to eliminate the problem of a possible hacking with firebug (in case someone changes the property “hidden”), I added a control:
    If the value of the field is different from the date of +6 months now returns an error “Please do not hack website”.

    Unfortunately I do not know why it always returns the error even if the value in the field is identical to the printed value (with echo)

    Sorry, I am not an expert in php and I could have made a mess.
    let me know
    SeeYa

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create/Update a custom field when a post is published’ is closed to new replies.