Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Replace an HTML element if logged in as a subscriber
<?php // Ignore this first line when copying to your child theme's functions.php file.
add_filter( 'wp_footer', function() {
if ( !is_user_logged_in() ) return; // Don't do anything if not logged in.
global $current_user;
$user_roles = $current_user->roles;
// var_dump($user_roles); // Uncomment to debug role(s).
$user_role = array_shift( $user_roles ); // Look at the first role. Change this if needed.
if ( $user_role !== "subscriber") return; // Bail early if we don't have a subscriber.
<script id="replace-node">
// Load this IIFE in the footer.
(function() {
// alert('hiya!'); // Uncomment for debugging.
// The code below replaces the compliments link with an empty div
// that has an ID of compliments-replacement-link. You can style
// that div to whatever you and and then add the '#compliments-replacement-link'
// selector as your extra CSS class for your Click Open trigger.
const div = document.createElement("div");
const text = document.createTextNode("CUSTOMIZE ME! ;-)");
div.setAttribute("id", "compliments-replacement-link"); // Create a replacement div. Customize this to what you want to launch your overriding popup.
const compLinkElt = document.querySelector("a[id^='compliments-']"); // Set the ID.
if ( compLinkElt ) compLinkElt.replaceWith(div); // If there's a compliments link, replace it with the div.
} );
* You can add the PHP code snippet to your child theme's functions.php file
* or with third-party plugins such as My Custom Functions and Code Snippets.
* Learn more:
* -
* -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment