-
I have 1,800 knitting recipes, that each come with a yarn recommendation (an affiliate links). Some yarns are recommended for many recipes, some for just a few.
Right now I’m using a Pretty Links-link (one for each yarn), which is inserted in the relevant posts. Over time yarn or suppliers have to be replaced, which is a tedious job.Is there a way to insert a [short code] in a post, so I just have to edit the short code definition – rather that opening the individual posts?
-
Hi there,
1. Add this PHP Snippet to your site:
add_shortcode('portable_hook', function($atts){ ob_start(); $atts = shortcode_atts( array( 'hook_name' => 'no foo' ), $atts, 'portable_hook' ); do_action($atts['hook_name']); return ob_get_clean(); });
2. then add the to the page the shortcode ie.
[portable_hook hook_name="inside-post-content"]
This will create a hook named:
inside-post-content
where the shortcode sits. You can change that to whatever you want.3. you can use a GP Block Element or Hook to hook content into the post.
3.1 in the Element set the Hook toCustom Hook
and in the field add your hook name eg.inside-post-content
3.2 Set the element display rules to Entire Site. -
Thanks. So having 300 different yarns, I create [portable_hook hook_name=”yarn-1″], [portable_hook hook_name=”yarn-2″], [portable_hook hook_name=”yarn-3″] etc. and insert them in the relevant posts. Are there any (performance) problems in having 300 different hooks?
-
Not really but its a nightmare to administrate. Perhaps this could be done dynamically.
Some things to consider:
How may Yarn Recommendation Affiliate links are there?
And how do you associate an affiliate with a knitting recipe? -
I have about to 250 yarn recommendations and 1,800 recipes, and I use Pretty Links. The link itself is inserted as shown on the image. If a supplier or a yarn is changed, I have to edit each post. Sometimes I can use search/replace. Unfortunately Pretty Links only support the URL – not the link (with link text).
To complicate things some recipes use two or even three yarns. -
hmmm…. using the Shortcode Hook doesn’t really add any value here.
Considering the large amout of them that would be required.
So a dynamic shortcode would be a better option.How are the Pretty Links added to the page ?
As I am unsure how you would output them from with the shortcode -
The affiliate link could be:
https://www.partner-ads.com/dk/klikbanner.php?partnerid=25924&bannerid=84733&htmlurl=https://kreativgarn.dk/product-category/producenter/sesia-hand-knitting/pura-lana-ecologica-sesia-hand-knitting/
I this case I use this short-URL:
https://strikkeglad.dk/kr-Sesia-Pura-Lana
I the actual post, this is the HTML:
<p><strong>Forslag til erstatningsgarn fra garnbutikken Kreativgarn: <a href="https://strikkeglad.dk/kr-Sesia-Pura-Lana">Sesia Pura Lana</a>.</strong></p>
(This shop is called Kreativgarn)
-
Ok, so you could do something like this:
1. create a function to store your array of Yarns:
function get_affiliate_links() { // Define an array of affiliate links with their respective data $affiliate_links = array( array( 'prefix' => 'Forslag til erstatningsgarn fra garnbutikken Kreativgarn:', 'link' => 'https://strikkeglad.dk/kr-Sesia-Pura-Lana', 'text' => 'Sesia Pura Lana' ), array( 'prefix' => 'Forslag til erstatningsgarn fra garnbutikken Kreativgarn:', 'link' => 'https://example.com/affiliate-link', 'text' => 'Example Yarn' ), // Add more affiliate links as needed ); return $affiliate_links; }
Here we store the prefix text, the link and the link text.
Just add more Array items for each affiliate.2. add this snippet to create a shortcode:
function affiliate_link_shortcode($atts) { // Extract shortcode attributes $atts = shortcode_atts(array( 'yarn-name' => '' ), $atts); // Get the array of affiliate links $affiliate_links = get_affiliate_links(); // Search for the affiliate link based on the provided yarn name foreach ($affiliate_links as $link) { if ($link['text'] === $atts['yarn-name']) { // Generate and return the HTML code for the affiliate link $link_html = '<p><strong>' . esc_html($link['prefix']) . ' <a href="' . esc_url($link['link']) . '">' . esc_html($link['text']) . '</a>.</strong></p>'; return $link_html; } } // If the provided yarn name is not found, return an error message return 'Affiliate link for ' . esc_html($atts['yarn-name']) . ' not found.'; } add_shortcode('affiliate_link', 'affiliate_link_shortcode');
This provides you this shortcode:
[affiliate_link yarn-name="Yarn Name"]
In here:
yarn-name="Yarn Name"
you add the value to match to thetext
value in the first function. -
Thanks so much for helping. I’ll check it out.
-
You’re welcome
-
I found out that Easy Affiliate Links uses shortcodes, so that’s a good solution here. Thanks.
-
Glad to hear you found a good solution!!
- You must be logged in to reply to this topic.