Resolved: Help! PHP exception based on URL

Hello, I don’t write PHP (yet) and would love someone to help me design a line or two. I have posted a complete php page below for a Wordpress template. By default the code causes any feature link to open in target="_blank"
I would like to change the default to target="_self" (this part I can do) but then I would like to make one link to an external site open using target="_blank"
I am happy to add individual site urls for any exceptions to the default rule. I have tried a few times to change myself but I am not experienced in PHP and don’t know the best way or correct code. Let me know if you need any more, many thanks in advance - Lanx

<?php
/**
 * The default template for displaying features entries
 *
 * @since Bizz 1.0
 */

//Vars
$wpex_feature_url = get_post_meta( get_the_ID(), 'wpex_feature_url', true ); ?>

<article id="id-<?php the_ID(); ?>"  <?php post_class(); ?>>
	<?php if ( $wpex_feature_url ) {
		echo '<a href="'. $wpex_feature_url .'" title="'. get_the_title() .'" target="_blank" class="feature-entry-url clr">';
	} ?>
	<?php if ( get_post_meta( get_the_ID(), 'wpex_icon_font', true ) ) { ?>
		<div class="feature-icon-font"><i class="fa fa-<?php echo get_post_meta( get_the_ID(), 'wpex_icon_font', true ); ?>"></i></div>
	<?php } elseif ( has_post_thumbnail() && get_theme_mod( 'wpex_blog_post_thumb', '1' ) == '1' ) { ?>
		<div class="feature-thumbnail">
			<img src="<?php echo wpex_get_featured_img_url(); ?>" alt="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" />
		</div><!-- .feature-thumbnail -->
	<?php } ?>
	<header class="feature-entry-header clr">
		<h2 class="feature-entry-title"><?php the_title(); ?></h2>
	</header>
	<div class="feature-entry-content entry clr">
		<?php the_content(); ?>
	</div>
	<?php if ( $wpex_feature_url ) {
		echo '</a>';
	} ?>
</article>
1 Like

Should be able to be (if you’re willing to hard code the specific URLs you want to be different)

<?php if ( ($wpex_feature_url === "<your_particular_url_here>") OR ($wpex_feature_url === "<another_particular_url_here>") ) {
		echo '<a href="'. $wpex_feature_url .'" title="'. get_the_title() .'" target="_blank" class="feature-entry-url clr">';
	}  else {
		echo '<a href="'. $wpex_feature_url .'" title="'. get_the_title() .'" target="_self" class="feature-entry-url clr">';
	} 
 ?>

Repeating the OR for as many particular URLs you have that you want to open specially.

I hope you realize that if you change the code here, the next time this theme comes out with an update, it will overwrite any changes you make, and you will have to do it all over again.

1 Like

@Lanxalot

Take a look here: https://www.elegantthemes.com/blog/resources/wordpress-child-theme-tutorial

Or just Google “How to create a child theme”. Modify the child theme, and not that main theme, so that if you update the main one, you won’t lose your custom code.

Many thanks for your help Jeffrey. I just posted the new code into the page (replacing the old section) with two specified urls to test it. The local links opened up fine using target=“self” as desired but unfortunately the specified url I wanted to open with target=“blank” also opened in the same window (self). Do you have any ideas why this might be? Thanks again for your time.

Hi WebMachine. Child themes are definitely top of the to do list, hopefully I will make a start tonight. Kind regards

@jeffreylees

Thanks for the links Jeffrey, I have two new sites to build and will definitely start to use child themes for them. They would certainly have saved me a few headaches in the past too. Thanks for the quick responses by the way, it’s my first time on a forum like this, the help is really appreciated.

1 Like

Did you replace the with the exact url as it is in that $wpex_feature_url variable? It would need to be exactly the same character for character, www., http or https, etc.

@jeffreylees
Hi Jeffrey, sure enough I had made a silly mistake. I’ll put it down to being tired. : ) I had accidentally left brackets in so “http://lanxphotography.com/” was “http://lanxphotography.com/

(I just realised the post got rid of these brackets < > but I left them in on the second link above. A silly mistake, easy to fix, I am very happy.)

The linked site also needs a lot of work but I’ll get there. I’m working on 5 at the moment. Thanks again for your help. I can now progress with the other tasks I have and the first will be a child site for 3 of my new websites. Much appreciated, have a great day!!!

p.s. Is there somewhere I should mark as resolved?

I changed the title for you - best we can do…

@DaveMaxwell

Many thanks Dave, much obliged

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.