Marketpress - Add Custom Fields without a plugin

Hello. I am interested in adding a custom field to the MarketPress single product on the wp-admin side for a youtube url

I would then like to be able to display that youtube embeded url in my single-product.php template which I have already designed.

I have found many posts that point to the CustomPress plugin but really I am hoping for a way to do this that does not require a plugin.

Can anyone provide me the
* a backend snippet for displaying the field within the Product Images meta section of the 'Add A Product' page
* a front end snippet displaying the contents of that field anywhere on the single-product.php page.


Jay Lepore

  • Adam Czajczyk

    Hello Jay,

    I hope you're well today and thank you for your question!

    Please create an empty file with a .php extension (e.g. "marketpress-youtube-field.php":wink: and put following code inside:

    function youtube_url_get_meta( $value ) {
    	global $post;
    	$field = get_post_meta( $post->ID, $value, true );
    	if ( ! empty( $field ) ) {
    		return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
    	} else {
    		return false;
    function youtube_url_add_meta_box() {
    		__( 'YouTube URL', 'youtube_url' ),
    add_action( 'add_meta_boxes', 'youtube_url_add_meta_box' );
    function youtube_url_html( $post) {
    	wp_nonce_field( '_youtube_url_nonce', 'youtube_url_nonce' ); ?>
    		<label for=&quot;youtube_url_url&quot;><?php _e( 'URL', 'youtube_url' ); ?></label><br>
    		<input type=&quot;text&quot; name=&quot;youtube_url_url&quot; id=&quot;youtube_url_url&quot; value=&quot;<?php echo youtube_url_get_meta( 'youtube_url_url' ); ?>&quot;>
    function youtube_url_save( $post_id ) {
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    	if ( ! isset( $_POST['youtube_url_nonce'] ) || ! wp_verify_nonce( $_POST['youtube_url_nonce'], '_youtube_url_nonce' ) ) return;
    	if ( ! current_user_can( 'edit_post', $post_id ) ) return;
    	if ( isset( $_POST['youtube_url_url'] ) )
    		update_post_meta( $post_id, 'youtube_url_url', esc_attr( $_POST['youtube_url_url'] ) );
    add_action( 'save_post', 'youtube_url_save' );

    Then save the file and upload it to the "/wp-content/mu-plugins" folder of your WP install. This will create a "YouTube URL" meta-box (see screenshot below) on product edit page.

    To output that URL in your template use following function (which is included in the code above):

    <?php echo youtube_url_get_meta( 'youtube_url_url' ); ?>

    Alternatively you may also use it this way:

    $yt_url = youtube_url_get_meta( 'youtube_url_url');
    // now do something with value $yt_url variable

    I created this entire code with an online meta box generator which is very handy tool and I think you may find it useful in future :slight_smile: You'll find it here:

    I hope that helps!

    Best regards,

  • Jay


    What can I say. If I could have prayed to the code Gods for the best possible solution your's would have exceeded my request :relieved:

    Worked perfectly and provided some education at the same time.

    I do have bit of info for you. I tried out your awesome metabox generator and noticed it came out with youtube_url_youtube_url even though the insert was just a single instance of youtube_url see attached image. I was able to remove the extra stuff and all tested perfectly. Not sure what's caching or duping it but you may wan to have a look. See attached image. The same thing happened to you as I see youtube_url_url in your code above.

    Thanks again !


  • Adam Czajczyk

    Hello Jay!

    If I could have prayed to the code Gods for the best possible solution your's would have exceeded my request

    No need for prayers, I merely knew what tool to use and showed it to you :slight_smile: But I'm really happy that I could help!

    As for those names (like youtube_url_url). I don't have any insights into a code powering that meta box generator but my bet here is that it's just a way to make sure that the meta name is unique (as it must be) and I think it's just combination of values of "Title" and "Label" fields.

    In my code it's "youtube_url_url" and in yours its "youtube_url_youtube_url". I can see that you used "youtube_url" value for "Label" file while I used just "URL". It's a usual practice to convert such values to lower case and replace spaces with underscores so as a result:

    - my case: "Youtube URL" (title) + "URL" label = "youtube_url_url"
    - your case: "Youtube URL" (title) + "youtube_url" label = "youtube_url_youtube_url"

    I hope that solves the mystery :slight_smile:

    Have a nice day!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.