admin管理员组文章数量:1123889
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/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 - / - for example.)
It should look like that:
Anchor text: StackOverflow Source link: /
All links need to have a nofollow attribute.
This is how it looks now: .png This is how I want it to look: .png
Does anyone know how can I modify it that way?
Kacper
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.sstatic.net/Kl7Ij.png This is how I want it to look: https://i.sstatic.net/OpMoS.png
Does anyone know how can I modify it that way?
Kacper
Share Improve this question asked Dec 3, 2018 at 17:24 kacper3355kacper3355 772 silver badges9 bronze badges 2- Hi Kacper. Do you still need help with this? – Sally CJ Commented Dec 5, 2018 at 0:15
- Hey! Yes, I still need help with this. :) Had no luck with answers. – kacper3355 Commented Dec 5, 2018 at 0:21
1 Answer
Reset to default 1You can add the extra (link/URL label) field like so:
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 theget_post_meta()
call when retrieving the field value.However, since there are now two fields, you may want to add a
placeholder
to theinput
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" />';
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'] );
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
?>
本文标签: customizationWordpresssource link pluginhow to modify it
版权声明:本文标题:customization - Wordpress - source link plugin - how to modify it? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736605878a1945327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论