Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:44:55+00:00 2026-06-04T07:44:55+00:00

I am trying to write a WordPress shortcode that will be smart enough to

  • 0

I am trying to write a WordPress shortcode that will be smart enough to find the closest image to the shortcodes placement inside the HTML view.

I would like to use jQuery to determine what the nearest image is and then pass it to the function below. I should mention that it is possible for the shortcode to be used multiple times in a post.

I have looked into using .closest() to achieve this but I am unsure how to pass this information to my function.

function pinterest_post() {

global $post;
$icon_url = urlencode(site_url().'/wp-content/themes/Kin/images/pinterest.png');
$posturl = urlencode(get_permalink());
$pinurl = 'http://pinterest.com/pin/create/button/?url='.$posturl.'&media='.$icon_url;
$pinurl .= '&description='.urlencode(get_the_title());

return '
    <div class="pinterest_post">
        <a href="'.$pinurl.'"><img src="/wp-content/themes/Kin/images/pinterest.png"/></a>
    </div>';
}

add_shortcode('pin', 'pinterest_post');

Any suggestions to achieve this would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:44:56+00:00Added an answer on June 4, 2026 at 7:44 am

    After rethinking my approach I found it to be the easiest to allow the user to specify the URL of the image as a parameter of the shortcode. This will ultimately give them greater control over which image is shown and would prevent having to transverse the DOM multiple times for each instance of [pin] in a post.

    My modified function and example usage:

    function pinterest_post($atts = '') {
    
        global $post;
    
        $image_fallback = get_bloginfo(template_url).'/images/logo-green-for-website.gif';
        $icon_url = get_bloginfo(template_url).'/images/pinterest.png';
    
        extract(shortcode_atts(array('path' => $image_fallback,), $atts));
        $image_url = $path;
    
        $posturl = urlencode(get_permalink());
    
        $pinurl = 'http://pinterest.com/pin/create/button/?url='.$posturl;
        $pinurl .= '&description='.urlencode(get_the_title());
        $pinurl .= '&media='.$image_url;
    
        return '
            <div class="pinterest_post">
                <a href="'.$pinurl.'"><img src="'.$icon_url.'"/></a>
            </div>';
    }
    
    add_shortcode('pin', 'pinterest_post');
    

    Usage inside a WordPress post:

    [pin path=”http://www.site.com/image.jpg”]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write a query that will find and display all of my
I'm trying to write a short Wordpress JQuery for Wordpress comments that would allow
Trying to write a couple of functions that will encrypt or decrypt a file
I'm trying to write a custom SQL query that will create a list of
I'm using WordPress and I'm trying to write a plugin that uses a file
I'm trying to write a Wordpress plug-in that automatically posts a blog post at
I'm new to writing WordPress plugins. I'm trying to write a little plugin that
I am trying to write a simple Wordpress plugin, which updates a custom field
So I'm trying to write a desktop app to publish posts to a Wordpress
I am trying write a function that generates simulated data but if the simulated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.