Selecteur de langue

Recently, I wanted to share an article published on Heropress on my site. I wanted it to appear in the list of publications with a direct link to the external resource. My challenge was sharing a direct link without creating an additional post that would display the link itself. I used the Post Formats ‘link’ feature to achieve this.

WordPress post formats

To address this need, I used WordPress’ native Post Formats feature (documentation).

This feature allows users to choose different layouts for their posts based on the type of content. In theory, each format can have a unique layout to customize the presentation of posts according to their content.

Note: Format display changes only if the active theme supports it.

Supported formats

  • Aside: Short texts with no formal title.
  • Gallery: Set of several images.
  • Link: Share links to other web pages.
  • Image: Publication of an image with little or no text.
  • Quote: Highlighting a quotation.
  • Status: Short status update.
  • Video: Publish a video.
  • Audio: Sharing audio files.
  • Chat: Sharing discussions in dialogue format.

Originally designed to provide different displays based on post format, this feature may seem outdated given the capabilities of the Gutenberg editor, especially when using block compositions. However, I find it useful for structuring my content and adding small functional enhancements.

Among the different post formats, the one that best fits my needs here is the link format.

Use link post format

To change the post format, select the desired format in the settings sidebar.

WordPress post editor showing the 'Post Format' selector set to 'Link'
WordPress post editor showing the ‘Post Format’ selector set to ‘Link’

If nothing appears, it’s because your theme does not support this feature. You need to enable it by adding the following code snippet to your theme’s or child theme’s functions.php file.

add_theme_support('post-formats', ['link']);Code language: JavaScript (javascript)

The selector appears, with the option of selecting ‘link’.

An external link as the post’s permalink

I based my functional design on the description of the link format in the official documentation.

Link: A link to another site. Themes may wish to use the first <a href=""> tag in the post content as the external link for that post. An alternative approach could be if the post consists only of a URL, then that will be the URL and the title (post_title) will be the name attached to the anchor for it.

The following code snippet retrieves the post’s content and assigns the first link found as the post’s permalink.

add_filter('post_link', 'hmwp_modify_permalink_external_link', 10, 2);
function hmwp_modify_permalink_external_link($url, $post) {
    if (has_post_format('link', $post)) {
        $content = apply_filters('the_content', $post->post_content);
        preg_match('/<a\s[^>]*?href=[\'"]([^\'"]*?)[\'"][^>]*?>/', $content, $matches);
        if (!empty($matches[1])) {
            $url = esc_url_raw($matches[1]);
        }
    }

    return $url;
}Code language: PHP (php)

Then, to publish an external resource on your site, such as an article, simply:

  • Create a new post.
  • Assign the link post format in the settings sidebar.
  • Paste the external link directly into the post content.

And that’s it 🤗

Display the format used in the dashboard columns

To provide a visual indication of post formats in the admin interface, I’ve added a column to the list of posts. Posts with the link format are labeled as link.

Creating a custom column

add_filter('manage_posts_columns', 'hmwp_add_post_format_column');
function hmwp_add_post_format_column($columns) {
    $screen = get_current_screen();
    if ($screen->post_type === 'post') {
        $columns['post_format'] = __('Post format', 'holdmywp');
    }

    return $columns;
}Code language: PHP (php)

Column contents

add_action('manage_posts_custom_column', 'hmwp_display_post_format_column_content', 10, 2);
function hmwp_display_post_format_column_content($column_name, $post_id) {
    if ($column_name === 'post_format') {
        if (has_post_format('link', $post_id)) {
            echo __('external link', 'holdmywp');
        } else {
            echo __('post', 'holdmywp');
        }
    }
}Code language: PHP (php)

If you want to handle all supported formats proactively:

add_action('manage_posts_custom_column', 'hmw_display_post_format_column_content', 10, 2);
function hmw_display_post_format_column_content($column_name, $post_id) {
    if ($column_name === 'post_format') {
        $post_format = get_post_format($post_id);
        
        switch ($post_format) {
            case 'aside':
                echo __('aside', 'text-domain');
                break;
            case 'gallery':
                echo __('gallery', 'text-domain');
                break;
            case 'link':
                echo __('link', 'text-domain');
                break;
            case 'image':
                echo __('image', 'text-domain');
                break;
            case 'quote':
                echo __('quote', 'text-domain');
                break;
            case 'status':
                echo __('status', 'text-domain');
                break;
            case 'video':
                echo __('video', 'text-domain');
                break;
            case 'audio':
                echo __('audio', 'text-domain');
                break;
            case 'chat':
                echo __('chat', 'text-domain');
                break;
            default:
                echo __('post', 'text-domain');
                break;
        }
    }
}Code language: PHP (php)

Full code extract

/*	-----------------------------------------------------------------------------------------------
    USE POST FORMAT LINK FOR EXTERNAL LINKS
--------------------------------------------------------------------------------------------------- */
add_filter('post_link', 'hmwp_modify_permalink_external_link', 10, 2);
function hmwp_modify_permalink_external_link($url, $post) {
    if (has_post_format('link', $post)) {
        $content = apply_filters('the_content', $post->post_content);
        preg_match('/<a\s[^>]*?href=[\'"]([^\'"]*?)[\'"][^>]*?>/', $content, $matches);
        if (!empty($matches[1])) {
            $url = esc_url_raw($matches[1]);
        }
    }

    return $url;
}

add_filter('manage_posts_columns', 'hmwp_add_post_format_column');
function hmwp_add_post_format_column($columns) {
    $screen = get_current_screen();
    if ($screen->post_type === 'post') {
        $columns['post_format'] = __('Post format', 'holdmywp');
    }

    return $columns;
}

add_action('manage_posts_custom_column', 'hmw_display_post_format_column_content', 10, 2);
function hmw_display_post_format_column_content($column_name, $post_id) {
    if ($column_name === 'post_format') {
        $post_format = get_post_format($post_id);
        
        switch ($post_format) {
            case 'aside':
                echo __('aside', 'text-domain');
                break;
            case 'gallery':
                echo __('gallery', 'text-domain');
                break;
            case 'link':
                echo __('link', 'text-domain');
                break;
            case 'image':
                echo __('image', 'text-domain');
                break;
            case 'quote':
                echo __('quote', 'text-domain');
                break;
            case 'status':
                echo __('status', 'text-domain');
                break;
            case 'video':
                echo __('video', 'text-domain');
                break;
            case 'audio':
                echo __('audio', 'text-domain');
                break;
            case 'chat':
                echo __('chat', 'text-domain');
                break;
            default:
                echo __('default', 'text-domain');
                break;
        }
    }
}Code language: PHP (php)

Author

Quentin Le Duff: Your WordPress partner

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *