3pepe3
3pepe3

Reputation: 613

WP Metabox on nav-menus for virtual pages not showing the url

I have some virtual pages on wordpress, and the page is stored on transient data when page is created or title changes. I'm trying to add those pages on the metabox on the nav-menus.php. Almost everything is working as expected but the url is missing on the backend and in the frontend the links are with an href="" Virtual pages The code :

<?php
class MP_MenuVirtualPages {
    public function __construct() {
        add_action('admin_init', array( $this, 'virtualpages_register_menu_metabox' ) ) ;
    }

    public function virtualpages_register_menu_metabox() {
        add_meta_box( 
            'virtualpages-menu-test-metabox', 
            __( 'Virtual pages', MP_TEXTDOMAIN ), 
            array( $this, 'render_manu_virtualpages_metabox' ), 
            'nav-menus', 
            'side', 
            'default'
        );
    }
    static public function render_manu_virtualpages_metabox( $object, $args ) {
        global $nav_menu_selected_id;
        global $wpdb;
        $sql = "SELECT `option_name` AS `name`, `option_value` AS `value`
        FROM  $wpdb->options
        WHERE `option_name` LIKE '%transient_gm_virtualpage%'
        ORDER BY `option_name`";

        $results = $wpdb->get_results( $sql );
        $virtualpages_items = array();
        foreach ( $results as $id => $result )
        {
            $page = unserialize( $result->value );
            $virtualpages_items[] = (object) array(
                'ID' => $id,
                'db_id' => 0,
                'menu_item_parent' => 0,
                'object_id' => 1,
                'post_parent' => 0,             
                'type' => 'virtualpages-custom-type',
                'object' => 'virtualpages-object-slug',
                'type_label' => '',
                'title' => $page['title'],
                'url' => home_url( $page['url'] ),
                'target' => '',
                'attr_title' => '',
                'description' => '',
                'classes' => array(),
                'xfn' => '',
            );
        }

        $walker = new Walker_Nav_Menu_Checklist( );

        ?>
        <div id="virtualpages-plugin-div">
            <div id="tabs-panel-virtualpages-plugin-all" class="tabs-panel tabs-panel-active">
                <ul id="virtualpages-plugin-checklist-pop" class="categorychecklist form-no-clear" >
                    <?php 
                    echo walk_nav_menu_tree( 
                        array_map( 'wp_setup_nav_menu_item', $virtualpages_items ), 0, (object) array( 'walker' => $walker )
                    ); 
                    ?>
                </ul>

                <p class="button-controls">
                    <span class="list-controls">
                        <a href="<?php
                        echo esc_url(add_query_arg(
                            array(
                                'virtualpages-plugin-all' => 'all',
                                'selectall' => 1,
                            )
                        ));
                        ?>#virtualpages-menu-test-metabox" class="select-all"><?php _e( 'Select All' ); ?></a>
                    </span>

                    <span class="add-to-menu">
                        <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-virtualpages-plugin-menu-item" id="submit-virtualpages-plugin-div" />
                        <span class="spinner"></span>
                    </span>
                </p>
            </div>
        </div>
        <?php
    }
}
new MP_MenuVirtualPages();

Any idea of what i'm missing ?

Upvotes: 0

Views: 517

Answers (1)

3pepe3
3pepe3

Reputation: 613

Solution:

the type on $virtualpages_items[] must be 'custom' and not 'type' => 'virtualpages-custom-type',

Upvotes: 1

Related Questions