Removing target=”_new” from postsmeta table

[ad_1]

Hi Javed,

Thank you for reaching out.

Could you please provide us with the page URL where we can check the hyperlink and a screen recording showing the steps you took to add the hyperlink in the Text Editor widget? You can use tools such as loom.com to record your screen.

Kind regards,

Hi,

I have managed to resolve this with a custom PHP script, in the posts meta table, Elementor serializes the content to JSON and I needed to remove the hyperlinks attributes from there.

Here is a copy of the script;

<?php
// Include WordPress to access $wpdb
require_once( 'wp-load.php' );

global $wpdb;

// Query the wp_postmeta table for posts with target="_new" or rel="noopener" in _elementor_data
$query = "
SELECT post_id, meta_value
FROM wpc5_postmeta
WHERE meta_key = '_elementor_data'
AND (meta_value LIKE '%target=%' OR meta_value LIKE '%rel=%')
LIMIT 500;
";

$posts = $wpdb->get_results( $query );

// Check if posts are returned
if ( empty( $posts ) ) {
echo "No matching posts found in wpc5_postmeta with '_elementor_data'.<br>";
exit;
}

echo "Found " . count( $posts ) . " posts to update.<br>";

// Loop through each post to process and update
foreach ( $posts as $post ) {
echo "<br>Processing post ID: " . $post->post_id . "<br>";

// Output the raw meta_value to see the format
echo "<strong>Raw meta_value:</strong><br>";
var_dump($post->meta_value);
echo "<br>";

// Decode the JSON data
$decoded_content = json_decode( $post->meta_value, true );

if ( json_last_error() === JSON_ERROR_NONE ) {
// Process the decoded content (if it's valid JSON)
array_walk_recursive( $decoded_content, function( &$value ) {
if ( is_string( $value ) ) {
// Replace target and rel attributes
$value = str_replace( 'target="_new"', '', $value );
$value = str_replace( 'rel="noopener"', '', $value );
}
});

// Re-encode the content as JSON
$updated_meta_value = json_encode( $decoded_content );

// Update the postmeta table with the new cleaned value
$wpdb->update(
'wpc5_postmeta',
array( 'meta_value' => $updated_meta_value ),
array( 'post_id' => $post->post_id, 'meta_key' => '_elementor_data' )
);

echo "Updated post ID: " . $post->post_id . "<br>";
} else {
echo "The content for post ID " . $post->post_id . " is not valid JSON.<br>";
}
}

echo "Processing completed successfully!";

I then had to regenerate the CSS and library, cleared cache, checked the content and it’s removed those hyperlink attributes.

So I don’t think I need to share a Loom recording for this.

I’ll mark this as resolved.

Thanks
Javed

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer