Skip to content

Export to register_post_type and register_taxonomy #4

Open

Description

@szepeviktor

Sometimes I use Pods to generate parameters (PHP code) for register_post_type() and register_taxonomy(). Then Pods will be disabled.

https://github.com/szepeviktor/wordpress-plugin-construction/blob/master/mu-pods-register-export/pods-register-export.php

Needs a one-line modification!

Activity

  1. sc0ttkclark commented on Oct 6, 2017

    @sc0ttkclark
    Member

    What modification was that exactly? Happy to bring your tweak over here.

  2. self-assigned this
    on Oct 6, 2017
  3. szepeviktor commented on Oct 6, 2017

    @szepeviktor
    Author

    Thanks.
    var_export instead of var_dump in pods_debug() to make it reversible.

    I know I could go pods_api()->load_pods... but it would need a big chunk of copy of the Pods plugin.

  4. sc0ttkclark commented on Oct 6, 2017

    @sc0ttkclark
    Member

    Where are you calling pods_debug() in that code?

  5. sc0ttkclark commented on Oct 6, 2017

    @sc0ttkclark
    Member

    Oh nevermind, I see now it's via the pods_debug_register param flag

  6. szepeviktor commented on Oct 6, 2017

    @szepeviktor
    Author

    Nowhere! 😄
    Its output is caught and reversed (eval-ed) into PHP arrays.

  7. szepeviktor commented on Oct 6, 2017

    @szepeviktor
    Author

    I would rewrite it if you give me instructions that will end up in under 100 lines of code.

  8. sc0ttkclark commented on Oct 6, 2017

    @sc0ttkclark
    Member

    I think what you came up with is clever, but this PR should make it easier to do what you want:

    pods-framework/pods#4498

    In Pods 2.7, you can hook into the pods_register_taxonomy and pods_register_post_type filters to collect all of the registrations that you'd like, the you can rebuild the register_post_type / register_taxonomy statements to use with your own export code easily from there.

    I'll add an option to export to register_post_type / register_taxonomy in this Export to Code plugin too.

  9. changed the title [-]Alternative for CPT-s and taxonomies only[/-] [+]Export to register_post_type and register_taxonomy[/+] on Oct 6, 2017
  10. szepeviktor commented on Oct 6, 2017

    @szepeviktor
    Author

    I'll start the rewrite as it is released on WP.org.

  11. szepeviktor commented on Oct 6, 2017

    @szepeviktor
    Author

    I'll add an option to export to register_post_type / register_taxonomy in this Export to Code plugin too.

    Thank you!

  12. jhmonroe commented on Mar 10, 2026

    @jhmonroe

    @sc0ttkclark Curious if this alternate export method was ever added?

    Today while testing this Pods export code to PHP plugin I found that adding the resulting PHP to a theme's functions.php threw a fatal error and does not enable those post types and fields in the admin. It still wants the plugin to be active?

    SCF (the ACF clone) exports standalone PHP but I'd prefer to use your more, ethical, project. :-)

    While trying to figure out what threw the fatal error, Claude also suggested rewriting the resulting PHP to use only register_post_type() and register_post_meta()

    Doing so enabled the exported code from Pods to work wonderfully as a standalone

    If Pods could export standalone code it would do 2 things:

    • enable Pods to be a helper plugin (the way create block theme and create content model are helper plugins for building themes)
    • enable the fields to all show up natively in the Block Bindings Attributes UI without needing to code the template tags

    Here's the resulting code (in my example it's for team bios):

    add_action( 'init', function() {
        register_post_type( 'bio', array(
            'label'         => 'Bios',
            'labels'        => array(
                'name'          => 'Bios',
                'singular_name' => 'Bio',
                'menu_name'     => 'Bios',
                'add_new'       => 'Add New Bio',
                'add_new_item'  => 'Add New Bio',
                'edit_item'     => 'Edit Bio',
                'view_item'     => 'View Bio',
                'all_items'     => 'All Bios',
            ),
            'public'            => true,
            'show_ui'           => true,
            'show_in_menu'      => true,
            'show_in_nav_menus' => true,
            'show_in_admin_bar' => true,
            'show_in_rest'      => true,
            'rest_base'         => 'bios',
            'menu_position'     => 0,
            'menu_icon'         => 'dashicons-businessman',
            'supports'          => array( 'title', 'thumbnail', 'custom-fields' ),
            'publicly_queryable' => true,
        ) );
    
        register_post_meta( 'bio', 'bio', array(
            'single'       => true,
            'type'         => 'string',
            'show_in_rest' => true,
            'label'        => 'Bio',
        ) );
    
        register_post_meta( 'bio', 'job_title', array(
            'single'       => true,
            'type'         => 'string',
            'show_in_rest' => true,
            'label'        => 'Job Title',
        ) );
    
        register_post_meta( 'bio', 'link_title', array(
            'single'       => true,
            'type'         => 'string',
            'show_in_rest' => true,
            'label'        => 'Link Title',
        ) );
    
        register_post_meta( 'bio', 'link', array(
            'single'       => true,
            'type'         => 'string',
            'show_in_rest' => true,
            'label'        => 'Link',
        ) );
    } );
    

    It's amazing cause then everything "just works" in the new Block Bindings UI:

    Image

    I think what you came up with is clever, but this PR should make it easier to do what you want:

    pods-framework/pods#4498

    In Pods 2.7, you can hook into the pods_register_taxonomy and pods_register_post_type filters to collect all of the registrations that you'd like, the you can rebuild the register_post_type / register_taxonomy statements to use with your own export code easily from there.

    I'll add an option to export to register_post_type / register_taxonomy in this Export to Code plugin too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions