Hello,
great plugin! I need to point the same note many times in a post, so I edit the code for version 1.3 to support ‘label’ and ‘ref’ optional arguments. For example [footnote label=”mylabel”]The footnote[/footnote] append new footnote at bottom of the post while [footnote ref=”mylabel”][/footnote] just add the hypertext link to the previous footnote without appending a new one. Here the patch:
diff -ruN a/assets/js/better-footnotes.js b/assets/js/better-footnotes.js
--- a/assets/js/better-footnotes.js 2020-06-07 22:43:06.000000000 +0200
+++ b/assets/js/better-footnotes.js 2021-09-21 15:17:55.000000000 +0200
@@ -11,6 +11,9 @@
betterFootnotes.options = Object.assign(betterFootnotes.options, betterFootnotesOptions);
}
+ betterFootnotes.fnItemsNum = {};
+ betterFootnotes.footnoteNum = 0;
+
betterFootnotes.populateFootnotesLists();
$(document).on('click', '.bfn-footnoteRef, .bfn-footnoteHook', function (e) {
@@ -42,13 +45,45 @@
var $footenotesList = $(".bfn-footnotesList", $footenotes);
$('.bfn-footnoteHook', containerSelector).each(function(index) {
- var footnoteNum = index + 1;
+ var generatefn = true;
+
+ var ref = $(this).attr('data-footnote-ref');
+ if (ref)
+ {
+ if (! betterFootnotes.fnItemsNum[ref])
+ {
+ alert('ERROR: "' + ref + '" reference not found!');
+ return;
+ }
+ var footnoteNum = betterFootnotes.fnItemsNum[ref]['fnNum'];
+ var generatefn = false;
+ }
+ else
+ {
+ var footnoteNum = betterFootnotes.footnoteNum + 1;
+ betterFootnotes.footnoteNum++;
+ }
var $footnoteHook = $(this);
+
+ var label = $(this).attr('data-footnote-label');
+ if (label && generatefn)
+ {
+ if (betterFootnotes.fnItemsNum[label])
+ {
+ alert('ERROR: duplicate label "' + label + '"');
+ return;
+ }
+ betterFootnotes.fnItemsNum[label] = {'fnNum': footnoteNum};
+ }
+
betterFootnotes.populateFootnoteHook($footnoteHook, footnotePostId, footnoteNum);
- var $footnoteItem = betterFootnotes.generateFootnoteItem($footnoteHook);
- $footenotesList.append($footnoteItem);
+ if (generatefn)
+ {
+ var $footnoteItem = betterFootnotes.generateFootnoteItem($footnoteHook);
+ $footenotesList.append($footnoteItem);
+ }
});
if (! $footenotesList.is(":empty")) {
diff -ruN a/assets/js/better-footnotes.min.js b/assets/js/better-footnotes.min.js
--- a/assets/js/better-footnotes.min.js 2020-06-07 22:43:06.000000000 +0200
+++ b/assets/js/better-footnotes.min.js 2021-09-21 15:17:55.000000000 +0200
@@ -1 +1 @@
-!function($){"use strict";var betterFootnotes={options:{scrollGap:0,scrollSpeed:0},init:function(){"undefined"!=typeof betterFootnotesOptions&&(betterFootnotes.options=Object.assign(betterFootnotes.options,betterFootnotesOptions)),betterFootnotes.populateFootnotesLists(),$(document).on("click",".bfn-footnoteRef, .bfn-footnoteHook",function(e){e.preventDefault();var target=$($(this).attr("href")+":first");target.length&&betterFootnotes.scrollTo(target,betterFootnotes.options.scrollSpeed)})},populateFootnotesLists:function(context){$(".bfn-footnotes",context).each(function(){betterFootnotes.populateFootnotesList(this)})},populateFootnotesList:function($footenotes){if(!($footenotes=$($footenotes)).data("is-populated")){var footnotePostId=$footenotes.attr("data-post-id"),containerSelector=$footenotes.attr("data-container");containerSelector=containerSelector||"#post-"+footnotePostId;var $footenotesList=$(".bfn-footnotesList",$footenotes);$(".bfn-footnoteHook",containerSelector).each(function(index){var footnoteNum=index+1,$footnoteHook=$(this);betterFootnotes.populateFootnoteHook($footnoteHook,footnotePostId,footnoteNum);var $footnoteItem=betterFootnotes.generateFootnoteItem($footnoteHook);$footenotesList.append($footnoteItem)}),$footenotesList.is(":empty")||$footenotes.show()}},populateFootnoteHook:function($footnoteHook,footnotePostId,footnoteNum){var footnoteId=betterFootnotes.generateFootnoteId(footnotePostId,footnoteNum),footnoteHookId=betterFootnotes.generateFootnoteHookId(footnotePostId,footnoteNum);return $footnoteHook.attr("id",footnoteHookId),$footnoteHook.attr("href","#"+footnoteId),$footnoteHook.attr("data-footnote-id",footnoteId),"numeric"===$footnoteHook.attr("data-footnote-type")&&$footnoteHook.text(footnoteNum),$footnoteHook},generateFootnoteHookId:function(postId,footnoteNum){return"article-footnote-hook-"+postId+"-"+footnoteNum},generateFootnoteId:function(postId,footnoteNum){return"article-footnote-"+postId+"-"+footnoteNum},generateFootnoteRef:function($footnoteHook){var $footnoteRef=$("<a></a>");return $footnoteRef.text($footnoteHook.text()),$footnoteRef.addClass("bfn-footnoteRef"),$footnoteRef.attr("href","#"+$footnoteHook.attr("id")),$footnoteRef.attr("id",$footnoteHook.attr("data-footnote-id")),$footnoteRef},generateFootnoteItem:function($footnoteHook){var $footnoteItem=$("<li></li>");return $footnoteItem.addClass("bfn-footnoteItem"),$footnoteItem.append(betterFootnotes.generateFootnoteRef($footnoteHook)),$footnoteItem.append(" "),$footnoteItem.append($footnoteHook.attr("data-footnote-content")),$footnoteItem},scrollTo:function(target,time){betterFootnotes.isDefined(time)&&!1!==time||(time=800);var targetY=target.offset().top-betterFootnotes.options.scrollGap;!1!==time||0!==time?$("html, body").animate({scrollTop:targetY},time,"easeOutCubic").promise().done(function(){}):$("html, body").scrollTop(targetY)},isDefined:function(object){return void 0!==object&&!1!==object}};jQuery.extend(jQuery.easing,{easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b}}),$(document).ready(betterFootnotes.init)}(jQuery);
\ No newline at end of file
+(function ($){"use strict";var betterFootnotes ={options:{scrollGap:0,scrollSpeed:0 },init:function (){if (typeof betterFootnotesOptions !== 'undefined'){betterFootnotes.options = Object.assign(betterFootnotes.options,betterFootnotesOptions);} betterFootnotes.fnItemsNum ={};betterFootnotes.footnoteNum = 0;betterFootnotes.populateFootnotesLists();$(document).on('click','.bfn-footnoteRef,.bfn-footnoteHook',function (e){e.preventDefault();var target = $($(this).attr('href') + ":first");if (target.length){betterFootnotes.scrollTo(target,betterFootnotes.options.scrollSpeed);} });},populateFootnotesLists:function(context){$('.bfn-footnotes',context).each(function(){betterFootnotes.populateFootnotesList(this);});},populateFootnotesList:function($footenotes){var $footenotes = $($footenotes);var isPopulated = $footenotes.data("is-populated");if (isPopulated){return;} var footnotePostId = $footenotes.attr("data-post-id");var containerSelector = $footenotes.attr("data-container");if (! containerSelector){containerSelector = "#post-" + footnotePostId;} var $footenotesList = $(".bfn-footnotesList",$footenotes);$('.bfn-footnoteHook',containerSelector).each(function(index){var generatefn = true;var ref = $(this).attr('data-footnote-ref');if (ref){if (! betterFootnotes.fnItemsNum[ref]){alert('ERROR:"' + ref + '" reference not found!');return;} var footnoteNum = betterFootnotes.fnItemsNum[ref]['fnNum'];var generatefn = false;} else{var footnoteNum = betterFootnotes.footnoteNum + 1;betterFootnotes.footnoteNum++;} var $footnoteHook = $(this);var label = $(this).attr('data-footnote-label');if (label && generatefn){if (betterFootnotes.fnItemsNum[label]){alert('ERROR:duplicate label "' + label + '"');return;} betterFootnotes.fnItemsNum[label] ={'fnNum':footnoteNum};} betterFootnotes.populateFootnoteHook($footnoteHook,footnotePostId,footnoteNum);if (generatefn){var $footnoteItem = betterFootnotes.generateFootnoteItem($footnoteHook);$footenotesList.append($footnoteItem);} });if (! $footenotesList.is(":empty")){$footenotes.show();} },populateFootnoteHook:function($footnoteHook,footnotePostId,footnoteNum){var footnoteId = betterFootnotes.generateFootnoteId(footnotePostId,footnoteNum);var footnoteHookId = betterFootnotes.generateFootnoteHookId(footnotePostId,footnoteNum);$footnoteHook.attr("id",footnoteHookId);$footnoteHook.attr("href","#" + footnoteId);$footnoteHook.attr("data-footnote-id",footnoteId);if ($footnoteHook.attr("data-footnote-type") === "numeric"){$footnoteHook.text(footnoteNum);} return $footnoteHook;},generateFootnoteHookId:function (postId,footnoteNum){return "article-footnote-hook-" + postId + "-" + footnoteNum;},generateFootnoteId:function (postId,footnoteNum){return "article-footnote-" + postId + "-" + footnoteNum;;},generateFootnoteRef:function ($footnoteHook){var $footnoteRef = $("<a></a>");$footnoteRef.text($footnoteHook.text());$footnoteRef.addClass("bfn-footnoteRef");$footnoteRef.attr("href","#" + $footnoteHook.attr("id"));$footnoteRef.attr("id",$footnoteHook.attr("data-footnote-id"));return $footnoteRef;},generateFootnoteItem:function ($footnoteHook){var $footnoteItem = $("<li></li>");$footnoteItem.addClass("bfn-footnoteItem");$footnoteItem.append(betterFootnotes.generateFootnoteRef($footnoteHook));$footnoteItem.append(" ");$footnoteItem.append($footnoteHook.attr("data-footnote-content"));return $footnoteItem;},scrollTo:function (target,time){if (!betterFootnotes.isDefined(time) || time === false){time = 800;} var targetY = target.offset().top - betterFootnotes.options.scrollGap;if (time !== false || time !== 0){$("html,body").animate({scrollTop:targetY },time,'easeOutCubic' ).promise().done(function (){});} else{$("html,body").scrollTop(targetY);} },isDefined:function (object){if (typeof object !== typeof undefined && object !== false){return true;} else{return false;} } };jQuery.extend(jQuery.easing,{easeOutCubic:function (x,t,b,c,d){return c * ((t = t / d - 1) * t * t + 1) + b;} });$(document).ready(betterFootnotes.init);})(jQuery);
\ No newline at end of file
diff -ruN a/src/BetterFootnotes/src/Main.php b/src/BetterFootnotes/src/Main.php
--- a/src/BetterFootnotes/src/Main.php 2020-06-07 22:43:06.000000000 +0200
+++ b/src/BetterFootnotes/src/Main.php 2021-09-21 15:17:55.000000000 +0200
@@ -124,6 +124,8 @@
$atts = shortcode_atts(
[
'type' => 'numeric',
+ 'label' => null,
+ 'ref' => null,
],
$atts,
'bfn_footnote'
@@ -135,7 +137,11 @@
$content = apply_filters('BetterFootnotes\footnoteContent', $content);
- $output .= '<a href="#bfn-footnotes-' . esc_attr($post->ID) . '" class="bfn-footnoteHook" data-footnote-type="' . esc_attr($atts['type']) . '" data-footnote-content="' . esc_attr($content) . '">';
+ // $output .= '<a href="#bfn-footnotes-' . esc_attr($post->ID) . '" class="bfn-footnoteHook" data-footnote-type="' . esc_attr($atts['type']) . '" data-footnote-content="' . esc_attr($content) . '">';
+ $output .= '<a href="#bfn-footnotes-' . esc_attr($post->ID) . '" class="bfn-footnoteHook" data-footnote-type="' . esc_attr($atts['type']) .
+ '" data-footnote-label="' . esc_attr($atts['label']) .
+ '" data-footnote-ref="' . esc_attr($atts['ref']) .
+ '" data-footnote-content="' . esc_attr($content) . '">';
$output .= Options::getOption('footnote_symbol');
$output .= '</a>';
]]>
Hi!
Great plugin that works great, however I cant find a way to change the “reference” text in the bottom, how do I do that?
Cheers!
https://hemfakta.se/