Skip to content

Instantly share code, notes, and snippets.

@kmaida
Last active June 30, 2020 20:31
Show Gist options
  • Select an option

  • Save kmaida/ed218a2c54f3d4f5012d8962a4275f9c to your computer and use it in GitHub Desktop.

Select an option

Save kmaida/ed218a2c54f3d4f5012d8962a4275f9c to your computer and use it in GitHub Desktop.

Revisions

  1. kmaida revised this gist Jun 30, 2020. 1 changed file with 54 additions and 51 deletions.
    105 changes: 54 additions & 51 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,62 +1,65 @@
    /**
    * Register "Activities" as a custom post type
    * Register Activities as a custom post type
    */
    function cpt_register_activities() {
    $labels = array(
    "name" => __( "Activities", "twentytwenty" ),
    "singular_name" => __( "Activity", "twentytwenty" ),
    "menu_name" => __( "Activities", "twentytwenty" ),
    "all_items" => __( "All Activities", "twentytwenty" ),
    "add_new" => __( "Add New Activity", "twentytwenty" ),
    "add_new_item" => __( "Add New Activity", "twentytwenty" ),
    "edit_item" => __( "Edit Activity", "twentytwenty" ),
    "new_item" => __( "New Activity", "twentytwenty" ),
    "view_item" => __( "View Activity", "twentytwenty" ),
    "view_items" => __( "View Activities", "twentytwenty" ),
    "search_items" => __( "Search Activities", "twentytwenty" ),
    "not_found" => __( "No Activities found", "twentytwenty" ),
    "not_found_in_trash" => __( "No Activities found in trash", "twentytwenty" ),
    "featured_image" => __( "Featured image for this activity", "twentytwenty" ),
    "set_featured_image" => __( "Set featured image for this activity", "twentytwenty" ),
    "remove_featured_image" => __( "Remove featured image for this activity", "twentytwenty" ),
    "use_featured_image" => __( "Use featured image for this activity", "twentytwenty" ),
    "archives" => __( "Activity archives", "twentytwenty" ),
    "insert_into_item" => __( "Insert into activity", "twentytwenty" ),
    "uploaded_to_this_item" => __( "Uploaded to this activity", "twentytwenty" ),
    "filter_items_list" => __( "Filter activities list", "twentytwenty" ),
    "items_list_navigation" => __( "Activities list navigation", "twentytwenty" ),
    "items_list" => __( "Activities list", "twentytwenty" ),
    "attributes" => __( "Activities Attributes", "twentytwenty" ),
    "name_admin_bar" => __( "Activity", "twentytwenty" ),
    "item_published" => __( "Activity published", "twentytwenty" ),
    "item_published_privately" => __( "Activity published privately", "twentytwenty" ),
    "item_reverted_to_draft" => __( "Activity reverted to draft", "twentytwenty" ),
    "item_scheduled" => __( "Activity scheduled", "twentytwenty" ),
    "item_updated" => __( "Activity updated", "twentytwenty" )
    $labels = array(
    "name" => __( "Activities", "twentytwenty" ),
    "singular_name" => __( "Activity", "twentytwenty" ),
    "menu_name" => __( "Activities", "twentytwenty" ),
    "all_items" => __( "All Activities", "twentytwenty" ),
    "add_new" => __( "Add New Activity", "twentytwenty" ),
    "add_new_item" => __( "Add New Activity", "twentytwenty" ),
    "edit_item" => __( "Edit Activity", "twentytwenty" ),
    "new_item" => __( "New Activity", "twentytwenty" ),
    "view_item" => __( "View Activity", "twentytwenty" ),
    "view_items" => __( "View Activities", "twentytwenty" ),
    "search_items" => __( "Search Activities", "twentytwenty" ),
    "not_found" => __( "No Activities found", "twentytwenty" ),
    "not_found_in_trash" => __( "No Activities found in trash", "twentytwenty" ),
    "featured_image" => __( "Featured image for this activity", "twentytwenty" ),
    "set_featured_image" => __( "Set featured image for this activity", "twentytwenty" ),
    "remove_featured_image" => __( "Remove featured image for this activity", "twentytwenty" ),
    "use_featured_image" => __( "Use featured image for this activity", "twentytwenty" ),
    "archives" => __( "Activity archives", "twentytwenty" ),
    "insert_into_item" => __( "Insert into activity", "twentytwenty" ),
    "uploaded_to_this_item" => __( "Uploaded to this activity", "twentytwenty" ),
    "filter_items_list" => __( "Filter activities list", "twentytwenty" ),
    "items_list_navigation" => __( "Activities list navigation", "twentytwenty" ),
    "items_list" => __( "Activities list", "twentytwenty" ),
    "attributes" => __( "Activities Attributes", "twentytwenty" ),
    "name_admin_bar" => __( "Activity", "twentytwenty" ),
    "item_published" => __( "Activity published", "twentytwenty" ),
    "item_published_privately" => __( "Activity published privately", "twentytwenty" ),
    "item_reverted_to_draft" => __( "Activity reverted to draft", "twentytwenty" ),
    "item_scheduled" => __( "Activity scheduled", "twentytwenty" ),
    "item_updated" => __( "Activity updated", "twentytwenty" )
    );
    $args = array(
    "labels" => $labels,
    "description" => "Add an ambassador activity / contribution.",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "activities",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => false,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "activity" ),
    "query_var" => "activities",
    "menu_position" => 5,
    "menu_icon" => "dashicons-megaphone",
    "supports" => array( "title", "custom-fields" )
    $args = array(
    "labels" => $labels,
    "description" => "Add an ambassador activity / contribution.",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "activities",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => false,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "activity" ),
    "query_var" => "activities",
    "menu_position" => 5,
    "menu_icon" => "dashicons-megaphone",
    "supports" => array( "title", "custom-fields" )
    );
    register_post_type( "activities", $args );
    register_post_type( "activities", $args );
    }
    add_action( 'init', 'cpt_register_activities', 0 );




    /**
    * Add "acf" property to REST API get and put
    * To support post, need to use https://github.com/airesvsg/acf-to-rest-api
  2. kmaida revised this gist Jun 30, 2020. 1 changed file with 59 additions and 0 deletions.
    59 changes: 59 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,62 @@
    /**
    * Register "Activities" as a custom post type
    */
    function cpt_register_activities() {
    $labels = array(
    "name" => __( "Activities", "twentytwenty" ),
    "singular_name" => __( "Activity", "twentytwenty" ),
    "menu_name" => __( "Activities", "twentytwenty" ),
    "all_items" => __( "All Activities", "twentytwenty" ),
    "add_new" => __( "Add New Activity", "twentytwenty" ),
    "add_new_item" => __( "Add New Activity", "twentytwenty" ),
    "edit_item" => __( "Edit Activity", "twentytwenty" ),
    "new_item" => __( "New Activity", "twentytwenty" ),
    "view_item" => __( "View Activity", "twentytwenty" ),
    "view_items" => __( "View Activities", "twentytwenty" ),
    "search_items" => __( "Search Activities", "twentytwenty" ),
    "not_found" => __( "No Activities found", "twentytwenty" ),
    "not_found_in_trash" => __( "No Activities found in trash", "twentytwenty" ),
    "featured_image" => __( "Featured image for this activity", "twentytwenty" ),
    "set_featured_image" => __( "Set featured image for this activity", "twentytwenty" ),
    "remove_featured_image" => __( "Remove featured image for this activity", "twentytwenty" ),
    "use_featured_image" => __( "Use featured image for this activity", "twentytwenty" ),
    "archives" => __( "Activity archives", "twentytwenty" ),
    "insert_into_item" => __( "Insert into activity", "twentytwenty" ),
    "uploaded_to_this_item" => __( "Uploaded to this activity", "twentytwenty" ),
    "filter_items_list" => __( "Filter activities list", "twentytwenty" ),
    "items_list_navigation" => __( "Activities list navigation", "twentytwenty" ),
    "items_list" => __( "Activities list", "twentytwenty" ),
    "attributes" => __( "Activities Attributes", "twentytwenty" ),
    "name_admin_bar" => __( "Activity", "twentytwenty" ),
    "item_published" => __( "Activity published", "twentytwenty" ),
    "item_published_privately" => __( "Activity published privately", "twentytwenty" ),
    "item_reverted_to_draft" => __( "Activity reverted to draft", "twentytwenty" ),
    "item_scheduled" => __( "Activity scheduled", "twentytwenty" ),
    "item_updated" => __( "Activity updated", "twentytwenty" )
    );
    $args = array(
    "labels" => $labels,
    "description" => "Add an ambassador activity / contribution.",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "activities",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => false,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "activity" ),
    "query_var" => "activities",
    "menu_position" => 5,
    "menu_icon" => "dashicons-megaphone",
    "supports" => array( "title", "custom-fields" )
    );
    register_post_type( "activities", $args );
    }
    add_action( 'init', 'cpt_register_activities', 0 );

    /**
    * Add "acf" property to REST API get and put
    * To support post, need to use https://github.com/airesvsg/acf-to-rest-api
  3. kmaida created this gist Jun 30, 2020.
    26 changes: 26 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /**
    * Add "acf" property to REST API get and put
    * To support post, need to use https://github.com/airesvsg/acf-to-rest-api
    * From https://stackoverflow.com/questions/56473929/how-to-expose-all-the-acf-fields-to-wordpress-rest-api-in-both-pages-and-custom
    */
    function create_ACF_meta_in_REST() {
    $postypes_to_exclude = ['acf-field-group','acf-field'];
    $extra_postypes_to_include = ["page"];
    $post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);

    array_push($post_types, $extra_postypes_to_include);

    foreach ($post_types as $post_type) {
    register_rest_field( $post_type, 'acf', [
    'get_callback' => 'expose_ACF_fields',
    'update_callback' => 'expose_ACF_fields',
    'schema' => null,
    ]
    );
    }
    }
    function expose_ACF_fields( $object ) {
    $ID = $object['id'];
    return get_fields($ID);
    }
    add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );