Docs    Twitter    Documentation

Add Custom JavaScript snippets for the X (formerly Twitter) plugin

How to add custom JavaScript alongside an X (Twitter) feed using WPCode, rather than editing theme files.

Would you like to add your own JavaScript to your X (formerly Twitter) feed?

In this guide, we’ll show you 2 ways to do it. The first uses the Custom JS box that’s built into the Custom Twitter Feeds plugin. The second uses WPCode, a free plugin from the makers of WPForms and All in One SEO. Both examples add a short note under your feed.

Before you get started, make sure the Custom Twitter Feeds plugin is installed and activated on your site.

Using the Custom JS Box

To get started, we’ll open the Custom JS box. It’s the simplest way to add JavaScript, because it’s part of the plugin.

In your WordPress dashboard, go to Twitter Feed » Settings. The plugin’s menus and screens still use the name Twitter. Then, click the Feeds tab.

The WordPress dashboard menu with Twitter Feed » Settings highlighted and an arrow pointing at the Feeds tab of the Settings page

On the Feeds tab, scroll down to the Custom JS box. Next, paste your code into it. For this example, we’ll paste code that adds a short note under your feed.

$( '.ctf' ).not( ':has(.my-feed-note)' ).append( '<p class="my-feed-note">Thanks for stopping by!</p>' );
The Custom JS box on the Feeds tab with the example code pasted in and an arrow pointing at the box

You don’t need to add any script tags. The plugin adds your code to the page for you. The $ sign works as a shortcut for jQuery.

At the bottom of the page, click Save Changes.

The bottom of the Settings page with an arrow pointing at the Save Changes button

Now, open a page that shows your feed. The note appears under it.

The bottom of an X feed on a page, with the note Thanks for stopping by! shown under the Load More button

The plugin runs your code once the feed has loaded. It runs it again each time more posts are added, such as when a visitor clicks the Load More button.

That’s why the example checks for the note first. Without that check, the note is added again each time.

Using WPCode

You can also add your JavaScript with WPCode. It’s a free plugin that keeps all of your custom code snippets in one place.

To begin, install and activate the WPCode plugin. Then, go to Code Snippets » Add Snippet.

The WordPress dashboard menu with Code Snippets » Add Snippet highlighted and the WPCode Add Snippet page open

On the Add Snippet page, hover over the box named Add Your Custom Code (New Snippet). Then, click + Add Custom Snippet.

The Add Your Custom Code box with an arrow pointing at its + Add Custom Snippet button

In the window that opens, choose JavaScript Snippet.

The list of code types, with an arrow pointing at the JavaScript Snippet box

First, type a title for your snippet. Then, paste your code into the code box.

For this example, we’ll use code that adds the same short note under your feed. Unlike the first example, it doesn’t use jQuery. This makes it safe on pages that don’t load it.

document.addEventListener( 'DOMContentLoaded', function() {
	document.querySelectorAll( '.ctf' ).forEach( function( feed ) {
		var note = document.createElement( 'p' );
		note.className = 'my-feed-note';
		note.textContent = 'Thanks for stopping by!';
		feed.appendChild( note );
	} );
} );
The snippet title Add a note under my X feed and the example JavaScript in the Code Preview box

You don’t need to add any script tags. WPCode adds them for you. The code box starts with a script line you can’t edit.

Next, find the Location field and click it. Then, choose Site Wide Footer. This adds your code at the bottom of your pages.

The Location menu open, with an arrow pointing at the Site Wide Footer option

At the top of the page, click the toggle to switch the snippet from Inactive to Active.

The top bar of the snippet page with an arrow pointing at the Inactive toggle

Then, click Save Snippet.

The top bar of the snippet page with the toggle set to Active and an arrow pointing at the Save Snippet button

Once the snippet is active, the note appears under your feed on the pages that show it, just like in the first example.

That’s it! You can now add your own JavaScript to your X (formerly Twitter) feed with the Custom JS box or a WPCode snippet. Next, you may want to see how to set up the Custom Twitter Feeds Pro plugin.

Frequently Asked Questions

Below, we’ve answered some of the most common questions about adding custom JavaScript to your feed.

Does the Custom JS box run on every page of my site?

The plugin adds your code to the footer of every page. It only runs on pages where a feed is shown, though. On other pages, nothing happens.

Why won’t my code save in the Custom JS box?

The plugin only saves Custom JS for WordPress users who are allowed to post unfiltered HTML. If your code doesn’t save, check that your user account has that permission.

Can I use the Custom JS box in the free version?

Yes. The free version of the plugin also has a Custom JS box on the Feeds tab of its Settings page.

support

Couldn’t find your answer in the docs?

Contact Support