Skip to content

Instantly share code, notes, and snippets.

@stephenscaff
Created July 19, 2016 22:35
Show Gist options
  • Save stephenscaff/178ea2f00dc50056a958e3e4110ec9be to your computer and use it in GitHub Desktop.
Save stephenscaff/178ea2f00dc50056a958e3e4110ec9be to your computer and use it in GitHub Desktop.

Revisions

  1. stephenscaff created this gist Jul 19, 2016.
    47 changes: 47 additions & 0 deletions acf-cpt-options.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /**
    * CPT ACF
    *
    * Adds a Menu Item for custom post types to add options page fields.
    *
    */

    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

    function ctp_acf_options_pages() {

    if( function_exists('acf_add_options_page') ) { //Check if installed acf

    $ctpacf_post_types = get_post_types( array(
    '_builtin' => false,
    'has_archive' => true
    ) ); //get post types

    foreach ( $ctpacf_post_types as $cpt ) {

    if( post_type_exists( $cpt ) ) {

    $cptname = get_post_type_object( $cpt )->labels->name;

    $cpt_acf_page = array(
    'page_title' => ucfirst( $cptname ) . ' Fields',
    'menu_title' => ucfirst( $cptname ) . ' Fields',
    'menu_slug' => 'cpt-acf-' . $cpt,
    'capability' => 'edit_posts',
    'position' => false,
    'parent_slug' => 'edit.php?post_type=' . $cpt,
    'icon_url' => false,
    'redirect' => false,
    'post_id' => 'cpt_' . $cpt,
    'autoload' => false
    );

    acf_add_options_page( $cpt_acf_page );

    } // end if
    }
    } else { //activation warning
    add_action( 'admin_notices', 'ctpacf_admin_error_notice' );
    }
    }

    add_action( 'init', 'ctp_acf_options_pages', 99 );