If you want to add a link outside your editor, you’ll need to code it into a custom function or add it directly in a theme file. Otherwise, you could create a new widget area and add the HTML for the link into the widget. Not a very efficient way to add links in your theme, but works nonetheless.
To add your links in a custom function, you could create the link in your editor and output your text to the front end using echo:
add_action('loop_start','text_links_the_wrong_way');
function text_links_the_wrong_way() {
echo'<div class="your-class"><a href="http://example.com">Link Text</a></div>';
}
A better and more efficient way is to use printf in your code like this:
add_action('loop_start','code_text_links_correctly');
function code_text_links_correctly() {
printf( '<a href="%s" class="your-class">' . __( 'Your link text here' ) . '</a>', __( 'http://example.com' ) );
}
Or, directly in a file like this:
<?php printf( '<a href="%s" class="your-class">' . __( 'Your link text here' ) . '</a>', __( 'http://example.com' ) ); ?>
printf without the %s
placeholder.
add_action( 'loop_start', 'printf_demo' );
function printf_demo() {
printf( __( 'If left unchecked, the post author link will not display globally.', 'genesis' ) );
}
Using printf with wp_kses
and a variable as your URL
$url = $audio_url;
$link = sprintf( wp_kses( __( '<a href="%s">Download</a>', '$text_domain' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $url ) );
echo $link;
Or, when hard coding a URL
$url = 'http://example.com';
$link = sprintf( wp_kses( __( '<a href="%s">Download</a>', '$text_domain' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $url ) );
echo $link;
Printf with $variable
printf( '<a href="%s" class="button download">' . __( 'Download' ) . '</a>', $audio_url );
sprintf With Images
add_action( 'genesis_after_header', 'after_header_banner' );
function after_header_banner() {
$output = sprintf( '<img class="after-header-banner" src="%s" alt="%s" />', get_stylesheet_directory_uri() .'/images/bg1.jpg', get_the_title( $post->ID ) );
echo $output;
}
printf with images
add_action( 'genesis_after_header', 'after_header_banner' );
function after_header_banner() {
printf( '<img class="after-header-banner" src="%s" alt="%s" />', get_stylesheet_directory_uri() .'/images/bg1.jpg', get_the_title( $post->ID ) );
}
Linking Image From Images Folder To Post Permalink
add_action( 'genesis_after_header', 'after_header_banner' );
function after_header_banner() {
$output = sprintf( '<a href="%s" class="after-header-banner"><img src="%s" alt="%s" /></a>', get_permalink(), get_stylesheet_directory_uri() .'/images/bg1.jpg', get_the_title( $post->ID ) );
echo $output;
}
Linking Featured Images To Permalink
if ( $image = genesis_get_image( array( 'format' => 'url', 'size' => 'thumbnail' ) ) ) {
printf( '<a href="%s" class="entry-image"><img src="%s" alt="%s" /></a>', get_permalink(), $image, the_title_attribute( 'echo=0' ) );
}
Linking Image To Home URL
add_action( 'genesis_after_header', 'from_child_themes_images_folder' );
function from_child_themes_images_folder() {
$image = sprintf( '%s/images/default.png', get_stylesheet_directory_uri() );
printf( '<a href="%s" class="your-class"><img src="%s"></a>', esc_url( home_url( '/' ) ), $image );
}
Linking Image From Customizer To Home URL
add_action( 'genesis_header', 'image_from_customizer' );
function image_from_customizer() {
printf( '<a href="%s" class="your-class"><img src="%s"></a>', esc_url( home_url( '/' ) ), get_header_image() );
}
Use with ternary
printf( genesis_html5() ? '<p class="entry-meta">%s</p>' : '<p class="byline post-info">%s</p>', do_shortcode( $post_info ) );
One reply on “Using printf & sprintf In WordPress – 12 Code Examples”
[…] How To Code Text Links Into WordPress Using printf – If you want to add a link outside your editor, you’ll need to code it into a custom function or add it directly in a theme file. Otherwise, you could create a new widget area and add the HTML for the link into the widget. […]