1

Throughout our site, I'd like to use a function to print the href of an anchor tag. The function parses query params and properly translates them on to a link to another site.

In functions.php, I've added:

add_filter( 'main_site_url', 'return_main_site_url' );
function return_main_site_url( $default_source = '' ) {
    $main_site_url = "https://www.example.com";

    if ( get_query_var('ws') ) {
        $main_site_url .= '?s=';
        $main_site_url .= get_query_var('ws');
    } elseif ( !empty($default_source) ) {
        $main_site_url .= '?s=';
        $main_site_url .= $default_source;
    }

    return $main_site_url;
}

I've tried to use the function from an HTML Contact widget in the sidebar like:

<a href="<?php echo apply_filters('main_site_url', wp_sidebar') $>" class="btn btn-danger">Learn More</a>

The output ends up NOT being processed:

<a href="&lt;?php echo apply_filters('main_site_url', 'wp_sidebar') ?&gt;" class="btn btn-danger">Learn More</a>

How do you go about using a function from an HTML widget on the sidebar?

brandonhilkert
  • 4,205
  • 5
  • 25
  • 38

1 Answers1

0

In functions.php

add_filter( 'main_site_url', 'return_main_site_url' );

    function return_main_site_url( $default_source = '' ) {
        $main_site_url = "https://www.example.com";

        if ( get_query_var('ws') ) {
            $main_site_url .= '?s=';
            $main_site_url .= get_query_var('ws');
        } elseif ( !empty($default_source) ) {
            $main_site_url .= '?s=';
            $main_site_url .= $default_source;
        }

        return $main_site_url;
      }

 add_shortcode('myshortcode','return_main_site_url');

and you use the [myshortcode] in your anchor tag and check the output.

Subhash Patel
  • 674
  • 7
  • 16
  • When I put the PHP tags in there it's like it's not going to allow it at all: https://www.dropbox.com/s/zq0w85lqh8sgmtb/Screen%20Shot%202018-11-13%20at%209.14.02%20PM.png?dl=0 – brandonhilkert Nov 14 '18 at 05:15
  • Remove the `` tag and check. – Subhash Patel Nov 14 '18 at 05:16
  • Please check this how to call function and return value: (https://wordpress.stackexchange.com/questions/93777/using-output-from-one-function-and-calling-it-into-another) – Subhash Patel Nov 14 '18 at 05:29
  • I answered the question above. Had to install a plugin or use a shortcode. Custom HTML widgets don't allow executable PHP out of the box. – brandonhilkert Nov 14 '18 at 05:46
  • No, you don't need a plugin for that you add this `add_shortcode( 'myshortcode', 'return_main_site_url' );` code in `functions.php` and use in your file – Subhash Patel Nov 14 '18 at 06:26
  • I'm aware that I don't need a plugin for a shortcode. Like I said, "Install a plugin OR use a shortcode". I wasn't suggesting you need a plugin to make a shortcode. – brandonhilkert Nov 15 '18 at 06:05