Created
April 22, 2025 14:05
-
-
Save mwender/f217b808b94649eefc8b5a8ff9960261 to your computer and use it in GitHub Desktop.
[Post Link Shortcode] Generates a hyperlink for the current post in WordPress. Works great with the Elementor Post Loop widget. #wordpress #elementor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generates a hyperlink for the current post, optionally using an external link, custom link text, or featured image. | |
* | |
* This shortcode can be used as [post_link], [post_link link_text="Custom Text"], or [post_link link_only="true" featured_image="true"]. | |
* - If the post has a meta field 'external_link', that URL will be used for the hyperlink instead of the permalink. | |
* - The link will open in a new tab if using an external link. | |
* - If 'link_only' is true, only the URL is returned. | |
* - If 'featured_image' is true, the post's featured image is returned as a linked <img> element. | |
* | |
* @since 1.0.0 | |
* | |
* @param array $atts { | |
* Optional. An array of shortcode attributes. | |
* | |
* @type string $link_text Optional. The link text to display. Defaults to the current post title. | |
* @type string $link_only Optional. If "true", only the URL is returned. Otherwise, full <h2><a>...</a></h2> markup is returned. Defaults to "false". | |
* @type string $featured_image Optional. If "true", return the post's featured image as a linked image. Defaults to "false". | |
* } | |
* @return string The generated hyperlink HTML, image, or just the URL based on the shortcode attributes. | |
*/ | |
function myplugin_post_link_shortcode( $atts ) { | |
global $post; | |
if ( ! isset( $post ) ) { | |
return ''; | |
} | |
$atts = shortcode_atts( | |
array( | |
'link_text' => get_the_title( $post ), | |
'link_only' => 'false', | |
'featured_image' => 'false', | |
), | |
$atts, | |
'post_link' | |
); | |
$external_link = get_post_meta( $post->ID, 'external_link', true ); | |
$link_url = ! empty( $external_link ) ? esc_url( $external_link ) : get_permalink( $post ); | |
$link_target = ! empty( $external_link ) ? '_blank' : '_self'; | |
$link_only = filter_var( $atts['link_only'], FILTER_VALIDATE_BOOLEAN ); | |
$featured_image = filter_var( $atts['featured_image'], FILTER_VALIDATE_BOOLEAN ); | |
if ( $link_only ) { | |
return esc_url( $link_url ); | |
} | |
if ( $featured_image ) { | |
if ( has_post_thumbnail( $post ) ) { | |
$image_html = get_the_post_thumbnail( $post, 'full' ); | |
return sprintf( | |
'<a href="%s" target="%s">%s</a>', | |
$link_url, | |
esc_attr( $link_target ), | |
$image_html | |
); | |
} else { | |
return ''; // No featured image to return | |
} | |
} | |
$link_text = esc_html( $atts['link_text'] ); | |
return sprintf( | |
'<h2><a href="%s" target="%s">%s</a></h2>', | |
$link_url, | |
esc_attr( $link_target ), | |
$link_text | |
); | |
} | |
add_shortcode( 'post_link', 'myplugin_post_link_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment