User avatar
×

User name

user email

Delete Account
Sign Out
BACK

Almost there!

We've sent an email to with a link to activate your account.
Contact us if you need any help.
BACK

Reset Password

Get New Password
BACK LOG IN

Create Your Account

By creating an account, you agree to the Widget Pro Terms of Service and Privacy Policy.
Register
BACK CREATE ACCOUNT

Login

Sign In

Widget Pro logo. Links to home.
Logo for Widget Pro. It shows an intertwined W and P character.
    User avatar
    ×

    User name

    user email

    Delete Account
    Sign Out
    BACK

    Almost there!

    We've sent an email to with a link to activate your account.
    Contact us if you need any help.
    BACK

    Reset Password

    Get New Password
    BACK LOG IN

    Create Your Account

    By creating an account, you agree to the Widget Pro Terms of Service and Privacy Policy.
    Make a free account
    BACK CREATE ACCOUNT

    Login

    Sign In
    Build a custom Duda widget that displays an author image and links to their author page

    Liz Fedak • Dec 13, 2021New Paragraph

    Tired of the default blog widgets? So was I! ;) Read this tutorial to learn how you can add a custom author bar widget

    0

    At the end of the day, your Duda website is made out of HTML, CSS, and JavaScript. We can tap into the HTML using JavaScript to manipulate what shows up on the blog page after it renders. Read the rest of the tutorial to learn how! You should have some familiarity with HTML, CSS, and JavaScript to understand what's happening in the code, but I'll give some high level context, so it's okay if you can't read code.


    All of this code should be added to a custom widget in the Duda widget builder. Check out Duda course on Building Custom Widgets if you need a quick introduction to the widget builder.


    Step 1. Find the author's name & generate the author URL

    Duda uses a data attribute in the HTML for the line that renders the author's name on your website. What is a data attribute? Using MDN's description: "Any attribute on any element whose attribute name starts with  data-    is a data attribute."


    This is what that looks like if you pull up a blog post on a Duda site and dig around in the HTML: [data-inline-binding='blog.composedAuthor'] (edited 4/27/23)


    Since we know that exists, we can use JavaScript to find that HTML tag to retrieve the name, i.e., the inner HTML of that tag. If you configured your blog widget to show the date, you might also need to add some code to split the string so you're only retrieving the author's name. (That's the `.innerText.split((' •')[0];` part of the example below.

    // JavaScript
    const details = document.querySelector("[data-inline-binding='blog.composedAuthor']").innerText.split('•')
    const name = details[0].trim();
    const pubDate = details[1].trim();

    Using the author's name, we can compose a link to their author page. The first option in the next JavaScript examples uses a "Text" input in the Duda widget builder's Content Editor tab with the variable name `urlBase`. If you're only using this on one site, you can skip that step and simply write in your URL, as you see in option 2.

    Great! Now we have variables with the author's name and a link to their author page. Not super useful out of context, but with a little more JavaScript magic, we can render the name and author image with a link to the author's page and remove the default author line.


    Step 2. Retrieve the author image URL

    For the sake of simplicity, in the demo we hard-coded an object that has a list of the author names and their profile images using a JavaScript object. To retrieve the author's image URL, we will look up the URL value using bracket notation on the `authors` object and assign it to a variable named `imageURL`.


    // JavaScript
    function getImage(name) {
    let person = data.config.authors.filter(x => x.name === name)[0]
    return person.image;
    }
    function getURL(name) {
    let person = data.config.authors.filter(x => x.name === name)[0]
    return person.url;
    }const authBlock = $(element).find('.author_img');
    const details = document.querySelector("[data-inline-binding='blog.composedAuthor']").innerText.split('•')
    const name = details[0].trim();
    const pubDate = details[1].trim();

    let imageURL = getImage(name)
    let namePage = name.toLowerCase().split(" ").join("-");
    let url = getURL(name)

    Step 3: Add HTML


    The HTML below will give some structure to the elements we are going to add to the DOM with JavaScript. You can change your HTML and classes to suit your preferences. This HTML is what we use on givityapp.com's blog post. Scroll up to check it out!


    The image URL you see below is a generic anonymous user image. When the blog page loads initially, it'll load with the anonymous profile image until the author's image loads.


    <div class="authorLine">

      <a href="" class="authorPageURL">

        <div><img src="https://genslerzudansdentistry.com/wp-content/uploads/2015/11/anonymous-user.png" class="blogAuthImg"><img></div>

        <div class="authName"><span class="bybeforename">By <span><span class="authorName"><span><div>

      <a>

    <div>

    Step 4: Add some more JavaScript

    Now we're ready to manipulate the DOM! We have access to the author's name, profile page, and their profile image.


    This example uses .find() to manipulate the HTML we added and to remove the original author line. There are a few things going on here, mainly, we are modifying attributes of those elements with the author data from the previous steps, and rendering new HTML using that data.


    // JavaScript
    function getImage(name) {
    let person = data.config.authors.filter(x => x.name === name)[0]
    return person.image;
    }
    function getURL(name) {
    let person = data.config.authors.filter(x => x.name === name)[0]
    return person.url;
    }

    const authBlock = $(element).find('.author_img');
    const details = document.querySelector("[data-inline-binding='blog.composedAuthor']").innerText.split('•')
    const name = details[0].trim();
    const pubDate = details[1].trim();

    let imageURL = getImage(name)
    let namePage = name.toLowerCase().split(" ").join("-");
    let url = getURL(name)

    $(element).find(".blogAuthImg").attr('src', new URL(imageURL))
    $(element).find(".authorPageURL").attr('href', url);
    $(element).find('.authorName').text(name)

    document.querySelector("[data-inline-binding='blog.composedAuthor']").remove();

     Step 5: Make it pretty!

    Disclaimer, I'm not a designer, so I consider this to be pretty. :) The CSS below will stylize the widget as you see in our author bar above. Modify the classes to suit your needs!

    :root {
    --auth-bar-height: 40px;
    }
    .author-line > * {
    font-family: 'PPNeueMachina-Light' !important;
    color: rgba(54, 73, 112, 1) !important;
    }
    .author-line {
    width: 250px;
    }
    .author-line > a {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: start;
    height: var(--auth-bar-height);
    width: 250px;
    gap: 20px;
    }

    .blogAuthImg {
    object-fit: cover;
    object-position: center center;
    width: var(--auth-bar-height);
    height: var(--auth-bar-height);
    display: block;
    border-radius: 100%;
    box-sizing: border-box;
    }
    .author-a {
    text-decoration: none !important;
    }

    .bybeforename {
    text-decoration: none !important;
    color: #000 !important;
    }

    .nameInjection {
    text-decoration: none !important;
    color: #35a5ad !important;
    white-space: nowrap;
    font-size: 16px;
    }
    By Liz Fedak 20 Jan, 2022
    Make life easier for your clients and make their sites faster with optimized images by uploading images to Duda from their Airtable base
    A small figurine holding a post-it note
    By Liz Fedak 17 Dec, 2021
    Learn how to use Airtable formulas to set up a publish date for your external collection items.
    By Liz Fedak 16 Dec, 2021
    Today's the official release of my website widgetpro.io, and it almost didn't happen! Read the story to hear about the epic adventure I went on today to ensure I could make this happen.
    By Liz Fedak 14 Dec, 2021
    Case study on my process when I built the custom widget shop on my website, Widgetpro.io
    By Liz Fedak 13 Dec, 2021
    Streamline your content collection process using Content Snare and use the Duda API to make a site directly from the Content Snare form when it's complete
    By Liz Fedak 13 Dec, 2021
    Learn how StoryWeb Creative used Duda's dynamic pages and custom widgets to manage weekly podcast content for the COPreneur Path Podcast
    Image of 4 letter cards spelling out the word blog
    By Liz Fedak 13 Dec, 2021
    Build a one-line Duda widget for extra blog functionality
    More Posts
    Share by: