• Resolved one3rdnerd

    (@one3rdnerd)


    It would be great if we could add a new field group for pages and then set some conditional settings, so it only shows if the parent is X or some other conditions are met. That way it would keep the edit screen for pages tidier and only show a specific meta box if needed.

    For example, I create a new field group for “Case Studies” but Case Studies are only used on Industry pages, which all have the parent page “Industries”.

    This sites arbitrarily on all pages and the user has to remember it doesn’t do anything unless the parent page is “Industries”.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support pdclark

    (@pdclark)

    <?php
    /**
    * Plugin Name: Hide Fields if not Industries
    */
    add_action(
    'admin_head',
    function(){
    // Are we not editing a post / page?
    if (
    ! array_key_exists( 'post', $_GET )
    || ! array_key_exists( 'action', $_GET )
    ) {
    // Don't do anything.
    return;
    }
    // Is the parent slug not 'industries'?
    $parent = get_post(
    get_post( $_GET['post'] )->parent
    );
    if (
    'industries' !== $parent->post_name
    ) {
    // Don't do anything.
    return;
    }
    ?>
    <style>
    /* Hide a specific field named "field-name" */
    .pods-form-ui-row-name-field-name,
    /* Hide a field group named "more-fields" */
    #pods-meta-more-fields
    {
    display: none;
    }
    </style>
    <?php
    }
    );
    Thread Starter one3rdnerd

    (@one3rdnerd)

    Thank you, that got me 90% of the way. I did have to change to post_parent as it’s a page.

    Here’s my final working code with the changed meta box div ID too.

    /**
    * Plugin Name: Hide Fields if not Industries
    */
    add_action(
    'admin_head',
    function () {
    // Only run on post.php edit screen.
    if (
    ! isset( $_GET['post'], $_GET['action'] )
    || 'edit' !== $_GET['action']
    ) {
    return;
    }

    $post_id = (int) $_GET['post'];
    $post = get_post( $post_id );

    if ( ! $post || 'page' !== $post->post_type ) {
    return;
    }

    // Get parent post.
    if ( ! $post->post_parent ) {
    // No parent, so parent is not "industries" -> hide meta box.
    $parent_slug = null;
    } else {
    $parent = get_post( $post->post_parent );
    $parent_slug = $parent ? $parent->post_name : null;
    }

    // Hide meta box if parent slug is NOT "industries".
    if ( 'industries' === $parent_slug ) {
    // Parent is "industries", so do nothing, leave visible.
    return;
    }
    ?>
    <style>
    /* Hide the Pods meta box when parent is not "industries" Change #pods-meta-case-studies to whatever your div ID is for your meta box. You can find this by inspecting the edit page screen */
    #pods-meta-case-studies {
    display: none;
    }
    </style>
    <?php
    }
    );
Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.