WordPress.org

Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#50424 new enhancement

Privacy Page navigation link

Reported by: leehodson Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Editor Keywords:
Focuses: Cc:

Description

The Privacy Page set in Dashboard > Settings > Privacy can be added to menus with ease but there is no easy way for non technical WordPress users to link to their site's correct Privacy Page within other areas of their site such as within widgets, pages and posts or within their site's footer.

Proposal

WordPress needs to include a Privacy Page shortcode that displays the correct link to a site's privacy page as set in Settings > Privacy. For example [privacy] and [privacy title=""].

Example code for this shortcode

<?php
function privacy_page_sc_vr51( $atts ) {
  # Creates a Privacy Page link shortcode.
  # Use as [privacy title="Privacy Page Title"] or [privacy]
  # Default page title is Privacy Page.
  # Page link points to the Privacy Page set in Dashboard > Privacy.
  # The privacy page will only display if the page is public (status = publish).
  # Place this snippet into the site theme's functions.php file, a custom functions plugin or some other 
  # suitable place.

        $atts = shortcode_atts(
                array(
                        'title' => 'Privacy Page'
                ), $atts
        );

        $name = sanitize_text_field( $atts['title'] );

        # See wp-admin/includes/class-wp-privacy-policy-content.php
        $privacy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );

        if ( ! empty($privacy_page_id) && get_post_status( $privacy_page_id ) == 'public' ) {
                return esc_url( get_page_link( $privacy_page_id );
        } else {
                return;
        }
}
add_shortcode( 'privacy', 'privacy_page_sc_vr51' );

Gist Link: https://gist.github.com/VR51/84b9ab89681d0677bd60d8b8565a6cb9

Use of this shortcode would ensure the link to the correct privacy page is displayed no matter how many times the page is switched in the site's settings and, in a WPML or similar setup, no matter which part of a site the link is displayed.

Attachments (1)

privacy-policy-link.png (13.3 KB) - added by pbiron 7 weeks ago.
example of how to link to the privacy policy page in the block editor (and Text widget)

Download all attachments as: .zip

Change History (10)

#1 follow-up: @carike
7 weeks ago

Hallo! :D
Welcome to Core Trac.

Have you considered submitting this as a plugin?

Also, have you considered that it may be more "future-proof" as a block?

#2 @leehodson
7 weeks ago

I tried to edit the ticket. Posted the wrong example code. It should be:

<?php
function privacy_page_sc_vr51( $atts ) {
        # Creates a Privacy Page link shortcode.
        # Use as [privacy title="Privacy Page Title"] or [privacy]
        # Default page title is Privacy Page.
        # Page link points to the Privacy Page set in Dashboard > Privacy.
        # The privacy page will only display if the page is public (status = publish).
        # Place this snippet into the site theme's functions.php file, a custom functions plugin or some other 
        # suitable place.

        $atts = shortcode_atts(
                                array(
                                                'title' => 'Privacy Policy'
                                ), $atts
                        );

        $title = sanitize_text_field( $atts['title'] );

        # See wp-admin/includes/class-wp-privacy-policy-content.php
        $privacy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );

        if ( ! empty($privacy_page_id) && get_post_status( $privacy_page_id ) == 'publish' ) {
                $link = get_privacy_policy_url();
                return "<a href=\"$link\" />$title</a>";
        } else {
                return;
        }
}

#3 in reply to: ↑ 1 @leehodson
7 weeks ago

Replying to carike:

Hallo! :D
Welcome to Core Trac.

Have you considered submitting this as a plugin?

Also, have you considered that it may be more "future-proof" as a block?

It's not really plugin territory. It's a short snippet. WordPress core provides the Privacy Page setting so should really offer an easy way for regular users to display the privacy page link in way that is always correct regardless changes made within Settings > Privacy.

This could be added as a block but that would not be helpful to users who wish to link to their privacy page in their site footer or widget area. Sounds like a lot of work too.

@pbiron
7 weeks ago

example of how to link to the privacy policy page in the block editor (and Text widget)

#4 @pbiron
7 weeks ago

The are 2 template tags than be used by themes/plugins: get_the_privacy_policy_link() and the_privacy_policy_link(), e.g., to add the privacy policy to the site's footer (or any other template).

Note: those template tags are not currently listed on List of Template Tags. I've inquired about the best way to get them list there.

That said, a non-technical user can easily add a link to the privacy policy page in the block editor (or a Text widget). Just type the text you want, click the Link icon on the block's toolbar, search for "privacy" (or whatever you named your privacy policy page) and select the privacy policy page...just as you would link to any other page/post from within the block editor.

Of course, after creating a link as above, if you then change which page is your privacy policy page, then you'd have to manually update the links.

Maybe a shortcode might be called for (a block would be nice, but since there still is no such thing as an "inline" block, a shortcode is probably more appropriate). But as @carike said, such a shortcode/block is probably plugin territory.

Last edited 7 weeks ago by pbiron (previous) (diff)

#5 @leehodson
7 weeks ago

As we all point out, the issue with adding the link manually is that when the page link changes any links added manually must be edited manually. A shortcode that grabs the link solves this issue.

I accept that theme and plugin authors could create their own shortcodes but this means lots of different developers adding their own snippet to call get_the_privacy_policy_link() in order to expose the same data. Adding a snippet to core would reduce plugin and theme code bloat and stop several dozen 'Privacy Link' plugins being added to the plugin directory.

#6 follow-ups: @knutsp
7 weeks ago

This is a good idea, but not implemented as a shortcode - it's about navigation. I think it belongs in the Navigation Link block, a new (inline) standalone version of it. Currently, this block may only be inserted into a Navigation Block (AFIK).

It should have a section with dynamic links to all special pages, like Home, Posts, Privacy (and plugin added special pages, like Cart etc.)

Shortcodes are not discoverable in the editor (without a plugin), and mostly a thing of the past.

Editor links inserter always inserts fixed links, defined at the time of insertion.

I would like to help writing a ticket, or re-purpose this, that petitions for an inline navigation link block, to handle all internal dynamic linking - something to insert in the text flow, or as an enhancement to the current link inserter.

#7 in reply to: ↑ 6 ; follow-up: @pbiron
7 weeks ago

Replying to knutsp:

This is a good idea, but not implemented as a shortcode - it's about navigation. I think it belongs in the Navigation Link block, a new (inline) standalone version of it. Currently, this block may only be inserted into a Navigation Block (AFIK).

I haven't used the Gutenberg plugin in a while, so wasn't aware of the Navigation Link block and that it was inline.

So, yeah, if you want to do a PR against Gutenberg with a mod to that block that would allow it to be used outside of a Navigation Block and provide a UI for selecting from some set of "standard" pages (such as the Privacy Policy page), then I say go for it (and reference this ticket in the PR). But it would need to be usable WITHIN a paragraph block...for the typical case of "See our Privacy Policy".

#8 in reply to: ↑ 6 @leehodson
7 weeks ago

I'm fine with whatever you feel is best to move this forward.

Makes sense, too. When the block editor is used globally within themes the Navigation Link block will/should be usable within non widgetised footer areas as well as within widget areas.

Replying to knutsp:

This is a good idea, but not implemented as a shortcode - it's about navigation. I think it belongs in the Navigation Link block, a new (inline) standalone version of it. Currently, this block may only be inserted into a Navigation Block (AFIK).

It should have a section with dynamic links to all special pages, like Home, Posts, Privacy (and plugin added special pages, like Cart etc.)

Shortcodes are not discoverable in the editor (without a plugin), and mostly a thing of the past.

Editor links inserter always inserts fixed links, defined at the time of insertion.

I would like to help writing a ticket, or re-purpose this, that petitions for an inline navigation link block, to handle all internal dynamic linking - something to insert in the text flow, or as an enhancement to the current link inserter.

#9 in reply to: ↑ 7 @knutsp
7 weeks ago

  • Component changed from Privacy to Editor
  • Summary changed from Privacy Page Shortcode to Privacy Page navigation link
  • Version 5.4.2 deleted

Replying to pbiron:

I haven't used the Gutenberg plugin in a while, so wasn't aware of the Navigation Link block and that it was inline.

My bad. It's not inline, not even standalone. Only accessible inside a Navigation block.

It has never been possible to create any kind real (dynamic) inline navigation in the editor, having to use plain "hard coded" links. The introduction of menues finally made it possible, in navigation areas (menues), to link to a page/post/archives so that it could follow them.

When navigation blocks (now in plugin only) appear as block for both full site and in editor, the lack of inline navigation links will hopefully be more visible as an oversight.

Gihub issue: https://github.com/WordPress/gutenberg/issues/23303

Note: See TracTickets for help on using tickets.