0

I'm working on a minor "project" on my wordpress site, and im trying to add a custom metabox to the formidable - edit post(page?) screen. But i seem to be unable to figure out how to do it.

add_action( 'add_meta_boxes', 'trustpilot_metabox', 10, 2 );
function trustpilot_meta_box($post_type, $post) 
{
    add_meta_box( 
        'trustpilot-metabox',
        __( 'Trustpilot Invitation' ),
        'trustpilot_metabox_callback',
        'formidable-entries',
        'right',
        'default'
    );
}

function trustpilot_metabox_callback()
{ 
    echo '<div>test</div>';
}

url looks like: https://teststore.swe/wp-admin/admin.php?page=formidable-entries&frm_action=edit&id=2222

I have searched google, and seem unable to come up with how to add it to a specific custom page - i have no problem adding it to any custom post or posts in general. I figured i was supposed to use the page name, but doesnt seem to work no matter what i put in

  • I created a plugin with the meta_box and then called it when the page has a certain page template. – Interactive Aug 7 '17 at 14:31
  • See my answer below – Interactive Aug 7 '17 at 14:39
0

Like this?

<?php

/*
Plugin Name: Your plugin name
Plugin URI: http://www.domain.com
Description: Description
Author: You obviously
Version: 1.0
*/

/**
* Adds a meta box to the post editing screen
*/

include('meta_box/meta_box.php');
?>

meta_box.php

<?php
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$page_temp = get_page_template_slug( $post_id );

if ($page_temp == 'template_name.php')
{
function home_custom_meta() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

// checks for post/page ID
    // I REMOVED THE EDITOR BECAUSE I DIDN'T NEED IT
    remove_post_type_support('page', 'editor');
    add_meta_box( 'home_meta', __( 'Homepage', 'home-textdomain' ), 'home_meta_callback', 'page' );

add_action('save_post','my_meta_save');
}
add_action( 'add_meta_boxes', 'home_custom_meta' );

/**
 * Outputs the content of the meta box
 */
function home_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'home_nonce' );

// CONTENT OF THE META BOX GOES HERE
}



/**
 * Saves the custom meta input
 */
function home_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'home_nonce' ] ) && wp_verify_nonce( $_POST[ 'home_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'post_value' ] ) ) {update_post_meta( $post_id, 'post_value', sanitize_text_field( $_POST[ 'post_value' ] ) );}

add_action( 'save_post', 'home_meta_save' );
}

/**
 * Adds the meta box stylesheet when appropriate
 */
function home_admin_styles(){global $typenow;
if( $typenow == 'page' ) {wp_enqueue_style( 'meta_box_styles', plugin_dir_url( __FILE__ ) . 'meta-box-styles.css' );}}
add_action( 'admin_print_styles', 'home_admin_styles' );
}

?>
  • you're calling get_page_template_slug on every front and back end request? you could probably make that more efficient. – Milo Aug 7 '17 at 15:17
  • Probably.... How about a suggestion then? – Interactive Aug 8 '17 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.