I'm using a little plugin that allows me to add source link to every article. I found the code in this thread: Article source link for posts

It's almost perfect, but I would like to modify it a little: first of all, I need to have one more field for link's anchor. Now there's just a "source link" field - when I paste a link here, it shows exactly the same way in post (for example: www.stackexchange.com/questions/21652pn626262pinnpznxpiqtq/.

I need an additional anchor field to be able to set a nice link's title. For example: StackOverflow (which leads to to the destinated article - https://wordpress.stackexchange.com/ - for example.)

It should look like that:

Anchor text: StackOverflow Source link: https://wordpress.stackexchange.com/

All links need to have a nofollow attribute.

This is how it looks now: https://i.imgur.com/JfN8UX6.png This is how I want it to look: https://i.imgur.com/qPuWiqp.png

Does anyone know how can I modify it that way?

Kacper

  • Hi Kacper. Do you still need help with this? – Sally CJ Dec 5 at 0:15
  • Hey! Yes, I still need help with this. :) Had no luck with answers. – kacper3355 Dec 5 at 0:21
up vote 1 down vote accepted

You can add the extra (link/URL label) field like so:

  1. In the wpse_source_meta_box() function, add the (HTML of the) extra field:

    echo '<input type="text" id="source-link-label"" name="source_link_label" value="'.
      get_post_meta( $post->ID, '_source_link_label', true ) .'" size="25" />';
    

    so as you can see, the field name is _source_link_label as used in the get_post_meta() call when retrieving the field value.

    However, since there are now two fields, you may want to add a placeholder to the input fields:

      echo '<input type="text" id="source-link"" name="source_link" value="'.
        get_post_meta( $post->ID, '_source_link', true ) .'" size="25" placeholder="URL" />';
    
      echo '<input type="text" id="source-link-label"" name="source_link_label" value="'.
        get_post_meta( $post->ID, '_source_link_label', true ) .'" size="25" placeholder="Label" />';
    

    Or add a <label> before the fields:

      echo '<label for="source-link">URL:</label> ';
      echo '<input type="text" id="source-link"" name="source_link" value="'.
        get_post_meta( $post->ID, '_source_link', true ) .'" size="25" />';
    
      echo '<label for="source-link-label">Label:</label> ';
      echo '<input type="text" id="source-link-label"" name="source_link_label" value="'.
        get_post_meta( $post->ID, '_source_link_label', true ) .'" size="25" />';
    
  2. In the wpse_source_link_save() function, save/update the field value like so:

    update_post_meta( $post_id, '_source_link_label', $_POST['source_link_label'] );
    
  3. And then use the field value (or the link label) along with the link URL, like so:

    <?php
    // Show if the source link was specified.
    if ( $url = get_post_meta( $post->ID, '_source_link', true ) ) :
        $label = get_post_meta( $post->ID, '_source_link_label', true );
        $label = $label ? $label : $url;
    ?>
        <div class="source-link">
            Source: <a href="<?php echo esc_url( $url ); ?>"><?php // wrapped (for clarity)
              echo esc_html( $label ); ?></a>
        </div>
    <?php endif; // end $url ?>
    

    (Part of the code was taken from here.)

So the full code, without the step #3's part, would look like this. :)

That should work, but I suggest you to use sanitize_text_field() when updating the fields:

update_post_meta( $post_id, '_source_link', sanitize_text_field( $_POST['source_link'] ) );
update_post_meta( $post_id, '_source_link_label', sanitize_text_field( $_POST['source_link_label'] ) );

See this article for more details on that.

UPDATE

You can use this code in place of what I gave in step #3 above, to make sure the source link is only shown on the last page of a multi-page content (split using <!--nextpage-->) — the link (if set) would always be shown on the first page if the content is not set to multi-page:

<?php
global $post, $pages, $page;

$total = count( $pages );
// Show if there's only one page, or that we're on the last page.
if ( $total < 2 || $page === $total ) :
    // Show if the source link was specified.
    if ( $url = get_post_meta( $post->ID, '_source_link', true ) ) :
        $label = get_post_meta( $post->ID, '_source_link_label', true );
        $label = $label ? $label : $url;
    ?>
        <div class="source-link">
            Source: <a href="<?php echo esc_url( $url ); ?>"><?php
              echo esc_html( $label ); ?></a>
        </div>
    <?php endif; // end $url
endif; // end last page check
?>
  • Wow, thank you so much!!! :) There's one more thing - all source links need to have nofollow attribute. What should I do in order to achieve it? :-) – kacper3355 Dec 5 at 3:10
  • You can do it like so: <a href="<?php echo esc_url( $url ); ?>" rel="nofollow"> – Sally CJ Dec 5 at 3:14
  • I mean, in step #3, just add the rel attribute to the <a> tag. But I guess you've figured it out. ;) And you can add any other (valid) attributes to that tag. – Sally CJ Dec 5 at 3:23
  • Perfect :) Thank you! Might I ask you one more question? Here's how the metabox looks like now: i.imgur.com/RhuZTrg.png - can we move the label above the text field? It looks bad if I change the label name to something in my language - i.imgur.com/lFHuh6t.png EDIT: Yeah, I figured it you - I'm not that bad :) – kacper3355 Dec 5 at 3:23
  • Yes, we can. But you could also just add a <br> (line break) after the URL field: echo '<input type="text" id="source-link"" name="source_link" value="'. get_post_meta( $post->ID, '_source_link', true ) .'" size="25" placeholder="URL" /><br>'; RE the EDIT: Ok, that's awesome! :) – Sally CJ Dec 5 at 3:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.