<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code - The Web Co. Ireland</title>
	<atom:link href="https://thewebco.ie/code/feed/" rel="self" type="application/rss+xml" />
	<link>https://thewebco.ie/code/</link>
	<description>eCommerce and Website Development Ireland</description>
	<lastBuildDate>Wed, 30 Oct 2024 10:00:51 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to Add Custom Citations and References in WordPress (with Shortcode Tutorial and Free Plugin)</title>
		<link>https://thewebco.ie/add-citations-references-wordpress-shortcode/</link>
					<comments>https://thewebco.ie/add-citations-references-wordpress-shortcode/#respond</comments>
		
		<dc:creator><![CDATA[Robert McMillen]]></dc:creator>
		<pubDate>Mon, 28 Oct 2024 12:21:41 +0000</pubDate>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Insights]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Tutorials]]></category>
		<guid isPermaLink="false">https://thewebco.ie/?p=1329</guid>

					<description><![CDATA[<p>Introduction to WordPress Citations Adding citations and references to WordPress posts can be essential for websites that handle academic content or research projects. In this tutorial, you’ll learn how to create a custom citation system in WordPress, complete with tooltips and a reference list, using shortcodes and simple PHP code. I created this for an&#8230;</p>
<p>The post <a href="https://thewebco.ie/add-citations-references-wordpress-shortcode/">How to Add Custom Citations and References in WordPress (with Shortcode Tutorial and Free Plugin)</a> appeared first on <a href="https://thewebco.ie">The Web Co. Ireland</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Introduction to WordPress Citations</h2>



<p>Adding citations and references to WordPress posts can be essential for websites that handle academic content or research projects. In this tutorial, you’ll learn how to create a custom citation system in WordPress, complete with tooltips and a reference list, using shortcodes and simple PHP code.</p>



<p>I created this for an open-source project called <em>The Énrí Ó Muirgheasa Project</em>, which documents the author&#8217;s research on Irish song and culture, including “Dhá Chéad de Cheoltaibh Uladh” (<em>Two Centuries of Ulster Song</em>). This method makes it easy to manage citations and references across WordPress sites, especially for extensive research or academic blogs.</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://thewebco.uk/wp-content/uploads/2024/10/Maighre-Dheas-na-Gruaige-Baine-Dha-Chead-de-Cheoltaibh-Uladh-1024x768.jpg" alt="Maighre Dheas na Gruaige Báine - Dhá Chéad de Cheoltaibh Uladh" class="wp-image-1343"/><figcaption class="wp-element-caption">See citations, bottom right hand page.</figcaption></figure>



<h2 class="wp-block-heading">Step 1: Create a Citation Shortcode</h2>



<p>A citation shortcode is ideal for marking references throughout your content, making citations easy to insert and display. We’ll create a shortcode called <code>[cite]</code> to use wherever we need a citation.</p>



<pre class="wp-block-code"><code>// Citation Shortcode
function simplest_citations_cite_shortcode($atts, $content = null) {
    static $cite_count = 0;
    $cite_count++;

    // Store the citation content for tooltip and &#91;citelist] shortcode
    global $citelist;
    $citelist&#91;$cite_count] = $content;

    // Citation link with tooltip data
    $cite_link = '&lt;sup&gt;&lt;a href="#cite-' . $cite_count . '" id="cite-link-' . $cite_count . '" class="cite-tooltip" data-tooltip="' . esc_attr($content) . '"&gt;&#91;' . $cite_count . ']&lt;/a&gt;&lt;/sup&gt;';
    
    return $cite_link;
}
add_shortcode('cite', 'simplest_citations_cite_shortcode');</code></pre>



<p>This code registers the <code>[cite]</code> shortcode, which keeps track of each citation number in the post and assigns it a unique tooltip.</p>



<h4 class="wp-block-heading">Usage Example:</h4>



<p>Place <code>[cite]Citation text here[/cite]</code> next to the text you want to reference.</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://thewebco.uk/wp-content/uploads/2024/10/Citation-shortcode-in-Wordpress-Gutenberg-editor-1024x686.png" alt="Citation shortcode in WordPress Gutenberg editor" class="wp-image-1344" style="aspect-ratio:16/9;object-fit:cover"/><figcaption class="wp-element-caption">Citation shortcode in WordPress Gutenberg editor</figcaption></figure>



<h2 class="wp-block-heading">Step 2: Create a Citations List Shortcode</h2>



<p>Now, let’s create a <code>[citelist]</code> shortcode to output all citations at the end of your content. This will loop through each citation created and display them in an ordered list.</p>



<pre class="wp-block-code"><code>// Citation List Shortcode
function simplest_citations_citelist_shortcode() {
    global $citelist;
    $output = '&lt;ol class="citation-list"&gt;';

    foreach ($citelist as $num =&gt; $citation) {
        $output .= '&lt;li id="cite-' . $num . '"&gt;' . $citation . ' &lt;a href="#cite-link-' . $num . '"&gt;&#91;Back]&lt;/a&gt;&lt;/li&gt;';
    }

    $output .= '&lt;/ol&gt;';
    return $output;
}
add_shortcode('citelist', 'simplest_citations_citelist_shortcode');</code></pre>



<p>To make this code re-useable, I also had the citations automatically appear after <em>the_content</em>, unless the [citelist] shortcode was used:</p>



<pre class="wp-block-code"><code>// Template Output<br>function simplest_citations_append_citelist($content) {<br>    global $citelist;<br><br>    // Only add the citations list if there are citations present<br>    if (!empty($citelist) &amp;&amp; strpos($content, '&#91;citelist]') === false) {<br>        $content .= '&lt;h3&gt;Citations&lt;/h3&gt;&#91;citelist]';<br>    }<br><br>    return $content;<br>}<br>add_filter('the_content', 'simplest_citations_append_citelist');</code></pre>



<p>This shortcode will create an organized list of references, ensuring that each citation is numbered and easy to navigate.</p>



<h2 class="wp-block-heading">Step 3: Automatically Append the Citation List</h2>



<p>To make this more user-friendly, we’ll add the citation list automatically after the content unless <code>[citelist]</code> is explicitly used in the post.</p>



<pre class="wp-block-code"><code>// Auto Append Citation List
function simplest_citations_append_citelist($content) {
    global $citelist;

    if (!empty($citelist) &amp;&amp; strpos($content, '&#91;citelist]') === false) {
        $content .= '&lt;h3&gt;Citations&lt;/h3&gt;&#91;citelist]';
    }

    return $content;
}
add_filter('the_content', 'simplest_citations_append_citelist');
</code></pre>



<p>This filter checks if citations are present and if <code>[citelist]</code> isn’t used manually; if so, it appends the list with a &#8220;Citations&#8221; heading.</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://thewebco.uk/wp-content/uploads/2024/10/Wordpress-Wiki-Style-Citations-1024x597.jpg" alt="" class="wp-image-1345"/><figcaption class="wp-element-caption">Citations automatically added to the end of post content, with a shortcode fallback.</figcaption></figure>



<h2 class="wp-block-heading">Step 4: Add Tooltip CSS and JavaScript</h2>



<figure class="wp-block-image size-large"><img decoding="async" src="https://thewebco.uk/wp-content/uploads/2024/10/Wiki-style-tooltip-citation-1024x277.jpg" alt="Wiki-style tooltip citation for WordPress" class="wp-image-1346"/><figcaption class="wp-element-caption">Wiki-style tooltip citation for WordPress</figcaption></figure>



<p>For better UX, we’ll style the citations with a tooltip that appears when users hover over them.</p>



<pre class="wp-block-code"><code>/* Tooltip CSS */
.cite-tooltip {
    position: relative;
    text-decoration: none;
    cursor: pointer;
	font-size: 0.8em;
}

.cite-tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    top: -80px;
    left: 50%;
    transform: translateX(-50%);
    width: max-content;
    max-width: 250px;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 12px;
    display: none;
    z-index: 1000;
}

.cite-tooltip:hover::after {
    display: block;
}</code></pre>



<h3 class="wp-block-heading">Tooltip JavaScript (jQuery):</h3>



<pre class="wp-block-code"><code>(function($) {
    'use strict';

    $(document).ready(function() {
        const citeLinks = $('.cite-tooltip');

        citeLinks.each(function() {
            $(this).on('mouseenter', function() {
                const tooltip = $(this).data('tooltip');
                $(this).attr('title', tooltip);
            });

            $(this).on('mouseleave', function() {
                $(this).removeAttr('title');
            });
        });
    });

})(jQuery);</code></pre>



<h2 class="wp-block-heading">Download and Extend</h2>



<p>This basic plugin structure is a good starting point. You can <a href="https://github.com/robbichin/simplest-citations" target="_blank" rel="noreferrer noopener">download it</a>, try it out on your WordPress site, and customize it further as needed. If you’re interested in extending this plugin or have any feedback, feel free to reach out!</p>
<p>The post <a href="https://thewebco.ie/add-citations-references-wordpress-shortcode/">How to Add Custom Citations and References in WordPress (with Shortcode Tutorial and Free Plugin)</a> appeared first on <a href="https://thewebco.ie">The Web Co. Ireland</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thewebco.ie/add-citations-references-wordpress-shortcode/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
