markjaquith / page-links-to

#WordPressPlugin: Lets you make a WordPress page (or other content type) link to an external URL of your choosing, instead of its WordPress URL.
GNU General Public License v2.0
110 stars 46 forks source link

Can't save in URL in Custom Post Types(editor conflict) #102

Closed earwickerh closed 4 years ago

earwickerh commented 4 years ago

Hi @markjaquith,I'm not able to use your plugin on custom post types on version 3.3.2. When I try and save the url value, the page reloads and the information disappears. I don't mean to double post but I'm not getting any errors in the console contrarily to the other bug someone mentioned.
This only happens in custom post types though, everything else works. In a comment on a related but potentially different bug, you mention" if you don't register your post type with right type of support, it can't save post meta via the REST API ".
As a test I tried activating all the types of supports to no avail. I also tried to explicitly set all supports as "false" in CPT UI, weirdly, this works. After more trial and error, I noticed your field for URL saves when i disable the editor support in the custom post type.

Why would the editor conflict with your plugin's saving ONLY on custom post type? Default posts save "page links to" field with editor enabled, custom post types don't save the field when editor is enabled.

Thanks for your help

Thanks!

psorensen commented 4 years ago

Hi @earwickerh. Can you confirm that your custom post type supports the REST API?

This code should output whether your CPT has rest support.

add_filter( 'register_post_type_args', function( $args, $cpt_name ) {
    if ( 'post' == $cpt_name ) {
        var_dump( $args['show_in_rest'] );
    }
    return $args;
}, 10, 2 );

see https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/ for more info on rest support for CPTs

markjaquith commented 4 years ago

@earwickerh can you share your CPT registration code? Are you having this issue with classic editor or block editor or both?

markjaquith commented 4 years ago

I tried with two CPTs like so:

<?php

add_action( 'init', function() {
    // Post type without show_in_rest = true.
    register_post_type( 'test-classic', [
        'label'                 => 'Classic Test',
        'labels' => [
            'name' => 'Classic Test Posts',
            'singular' => 'Classic Test Post',
        ],
        'supports'              => ['title', 'editor'],
        'public'                => true,
        'capability_type'       => 'page',
    ]);

    // Post type with show_in_rest = true.
    register_post_type( 'test-block', [
        'label'                 => 'Block Test',
        'labels' => [
            'name' => 'Block Test Posts',
            'singular' => 'Block Test Post',
        ],
        'supports'              => ['title', 'editor'],
        'public'                => true,
        'capability_type'       => 'page',
        'show_in_rest'          => true,
    ]);
});

One supports classic editor only. The other turns on show_in_rest which enables the block editor. Page Links To shows up and works properly for both types.

Both types have editor support enabled.

@earwickerh what are you doing differently? Can you try with my test CPTs above and see if it works? And again, if you can share your CPT registration code that would be very helpful!

edvinkuric commented 4 years ago

Hey @markjaquith, i have the same issue - cannot save a custom-url when using CPT in gutenberg-editor, but works for "normal" pages/posts. I used the following configuration to create the custom post type:

/* Custom Post Type-Function*/
function create_posttype($key, $name, $singularName, $capability_type = 'post')
{
    register_post_type($key,
        // CPT Options
        array(
            'labels' => array(
                'name' => __($name),
                'singular_name' => __($singularName),
            ),
            'public' => true,
            'has_archive' => false,
            'rewrite' => array('slug' => $key),
            'show_in_rest' => true,
            'supports' => array('title', 'revisions', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'page-attributes'),
            'hierarchical' => false,
            'taxonomies' => array('category'),
            'capability_type' => $capability_type
        )
    );
}

function create_posttype_intranet()
{
    create_posttype('intranet', 'Intranet', 'Intranet');
}

add_action('init', 'create_posttype_intranet');

For me, this problems occurs also with the capability_type 'page'. if you need help with fixing this issue, i can maybe create a pull-request if you can point me to the right files :)

Thank you very much.

sergei-lobanov commented 4 years ago

Hi @markjaquith I have the same issue I'm using Elementor, but it doesn't work for default WP editor as well.

Not sure how to fix it, I found only your plugin with support of opening links in a new windows... Please have a look when you have a time.

johnpennypacker commented 4 years ago

I think this is related to execution order. The register_hooks() method (Line 191 of classes/plugin.php), tries to register meta fields to all CPTs, but it actually only registers fields to CPTs that have been registered so far. It executes before my init action uri_cpt executes, so my CPTs never get the memo about the meta fields. I presume that if init priority is equal, execution order is alphabetical.

As a workaround, if I add the third argument to add_action to set priority, it works.

add_action('init', 'uri_cpt', 9);

edvinkuric commented 4 years ago

@johnpennypacker that's true, I ended up with setting the third argument to 0, so that shouldn't be a problem for now.

markjaquith commented 4 years ago

I'm going to move that registration to init:9999 and also do it again at rest_api_init:9999 to catch any stragglers.