Support » Developing with WordPress » add call to action button within picture

  • iago2

    (@iago2)


    Hello,

    I have installed the nanospace theme. I can customize the header by adding a picture. (header media) On that picuture then the title of the front page will be displayed. Is it possible to add a call to action button beneath the title (on that picture)?

    Principally, I can look up the code for the header and make the adjustments there but I do not want to break anything.
    How would a wordpress theme developer approach this task?

    Thanks a lot, iago

    • This topic was modified 1 month ago by iago2.
    • This topic was modified 1 month ago by iago2.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi there! Themes tend to each handle this slightly differently. In the case of the NanoSpace theme, its description mentions “header builder” functionality, so it’s possible that it includes that option already. You may have luck asking in their forum here:

    https://wordpress.org/support/theme/nanospace/

    Moderator bcworkz

    (@bcworkz)

    Assuming nanospace uses the_title() to output post titles, you can use “the_title” filter to add additional HTML to appear immediately afterwards. You may also need added CSS to get everything looking right. Custom code like this can reside in a site specific plugin you create, or a child theme.

    iago2

    (@iago2)

    Thanks a lot for the answers, I’ve also opened a thread at the nanospace forum.

    I googled for the_title() and I got this function here:

    the_title( string $before = '', string $after = '', bool $echo = true )

    Where do I find that function and how do I add the markup, just adding it as a string parameter for the_title?

    • This reply was modified 1 month ago by iago2.
    Moderator bcworkz

    (@bcworkz)

    The parameters passed to the function are meant for wrapping the title in theme specific containers. While you could alter your theme template’s call to include your HTML, that is not an optimal approach if you’ve no other need to alter the template.

    Also, if you’re altering the template anyway, just add your HTML to where ever you want it. My earlier suggestion involves hooking “the_title” filter (not function). Filter hook code can reside in a child theme or custom plugin so you’re not required to alter your theme’s code.

    Generic information on filter hooks:
    https://developer.wordpress.org/plugins/hooks/filters/

    For context, I find it helpful to see the source code where the filter is applied. At the end of the function declaration:
    https://developer.wordpress.org/reference/functions/get_the_title/#source

    What your filter callback returns is in turn returned by the function, which was called by the_title(), where the return value is then echoed out.

    Thanks @bcworkz for your precise answer.

    protected_title_format, private_title_format, the_title, these are all callback functions within the get_the_title function and apply_filters() is the filter-hook for managing the callbacks, right?

    What are these callbacks good for and how do they help me e.g. to add an extra button to the header/header image?

    Thanks, iago

    Moderator bcworkz

    (@bcworkz)

    They are not callbacks in themselves, they are filter “tags” used to identify where added callbacks should be assigned or retrieved (in a global array) when apply_filters() or add_filter() is called. Other theme or plugin code adds their own callback functions using these tags as reference. You need to look at the source code where the filters are applied to understand which might be appropriate for what. I don’t think the *_format filters will be useful to you for adding something after the title. Use “the_title”.

    Your callback gets passed some value that you would want to modify, sometimes along with other supporting data that might be useful. Whatever your callback returns is what is used in subsequent code. For “the_title” filter, you are passed the title HTML and the post’s ID. Maybe the passed value is <h2>Hello World!</h2> for example. Your callback could concatenate button HTML after that and end up returning <h2>Hello World!</h2><input type="button" value="Lets do it!">. That will end up being what WP outputs when the_title(); is called from the template.

    This filter concept is admittedly rather tricky to understand at first. Once you do fully understand it though, it’ll be hard to look back and understand what was so tricky about it. It’s only becomes simple once you understand it. It is an important concept to grasp. Filters and the very similar action hooks are the primary way we alter the way WP works to our liking.

    iago2

    (@iago2)

    Ok, I surely will understand the filter concept one day. But isn’t it a bit overkill, to start with these filters, if I just want to add a simple html-button?

    By the way if I just add some more html code to the $titel – parameter of the apply filter, where do I add the additional HTML? And I also should give it some styling?? Where do I add my CSS?

    iago2

    (@iago2)

    I think this is the function that renders that header image (it is really not clear if the image belongs to the header or already to the page):

    the file is called: class-intro.php

    code:

    * 50) Special intro
    	 *
    	 * Front page special intro wrapper: open
    	 *
    	 * @since 1.0.0
    	 * @version 1.0.0
    	 */
    	public static function special_wrapper_open() {
    
    		// Requirements check.
    		if ( ! is_front_page() || NanoSpace_Post::is_paged() ) {
    			return;
    		}
    
    		echo '<div class="intro-special">';
    
    	} // /special_wrapper_open
    
    	/**
    	 * Front page special intro wrapper: close
    	 *
    	 * @since 1.0.0
    	 * @version 1.0.0
    	 */
    	public static function special_wrapper_close() {
    
    		// Requirements check.
    		if ( ! is_front_page() || NanoSpace_Post::is_paged() ) {
    			return;
    		}
    
    		echo '</div>';
    
    	} // /special_wrapper_close
    
    	/**
    	 * Setting custom header image as an intro background for special intro
    	 *
    	 * @uses  <code>nanospace_esc_css</code> global hook
    	 *
    	 * @since 1.0.0
    	 * @version 1.0.0
    	 */
    	public static function special_image() {
    
    		// Pre.
    		$disable = (bool) apply_filters( 'nanospace_intro_disable', false );
    
    		$pre = apply_filters( 'nanospace_intro_special_image_pre', $disable );
    
    		if ( false !== $pre ) {
    			if ( true !== $pre ) {
    				echo $pre; // Method bypass via filter.
    			}
    
    			return;
    		}
    
    		if ( $css = self::get_special_image_css() ) {
    			wp_add_inline_style(
    				'nanospace',
    				(string) apply_filters( 'nanospace_esc_css', $css . "\r\n\r\n" )
    			);
    		}
    
    	} // /special_image
    Moderator bcworkz

    (@bcworkz)

    Yes, it is a bit overkill from one perspective. But using filters can save us from altering the theme’s original code. If we altered the original code, our alterations would be lost when the theme updates. It’s worth the “overkill” to avoid needing to reapply our customization after any update. Simple filter hooks could be one liners, so not that much more involved than directly adding button HTML to the template.

    With a child theme to protect your custom work, you could safely alter a template file copy instead of using filter hooks if you prefer. Filters can also be added from plugins. Plugins are a little easier to setup than child themes. There are a few ways to approach this. This example uses the filter to add an input button after the title:

    add_filter('the_title', function( $title ){
      return $title . '<input id="do-it" type="button" value="Lets do it!">';
    });

    N.B. the button doesn’t do anything on its own, it needs JavaScript to listen for a click event and to do something. Alternately, append a complete form element which submits to somewhere meaningful.

    You can place CSS to style the button in a child theme’s style.css file, or add it to the current theme’s Additional CSS customizer section.

    The code you posted does appear to set the header image as a header background element. But the code responsible for the title over the image lies elsewhere. I’m not familiar with nanospace. I’m not even sure it uses the_title(), though it’s a reasonable bet. From the code you posted, nanospace does appear to offer plenty of filter hook opportunities. That’s a good sign. Even if “the_title” doesn’t work for any reason, there’s likely something that can be used.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.