Home › Forums › Pro Themes › Photomania Pro › Open all external links in new window
Tagged: external link, link
- This topic has 18 replies, 3 voices, and was last updated 4 years, 7 months ago by stiver1.
-
AuthorPosts
-
March 29, 2017 at 1:56 am #37153EZiserParticipant
Hi WEN,
I’m having trouble finding an answer for this – all results seem to be theme specific.
What snippet of code can I add to PhotoMania Pro so all external links open in a new tab? I’d like to do this without adding another plugin.
Thanks!
March 29, 2017 at 2:44 am #37160wensolutionsKeymasterHello @eziser,
If you want all of your external link to open in a new tab you have to include js code .
For this you can go through the child theme approach or add an external plugin.
Below is the link to one of the plugin :
https://wordpress.org/plugins/custom-css-js/
Then include below given js code .
jQuery(document).ready(function($){ $("a[href^=http]").each(function(){ if(this.href.indexOf(location.hostname) == -1) { $(this).attr({ target: "_blank", title: "Opens in a new window" }); } }) });
Note: We cannot assure 100% compatibility of the plugin we have recommend to the theme as we have not tested the compatibility of the plugin to the theme .
Hope this helps.
Best Regards!!
March 29, 2017 at 4:41 am #37228EZiserParticipantHi WEN,
Thanks for your prompt response. Where do I add the jc code in the child theme?
To reiterate: this must be done WITHOUT a plugin; particularly one that hasn’t been tested.
March 29, 2017 at 5:32 am #37244wensolutionsKeymasterHello @eziser,
To include the js code in the child theme you have to enqueue the js script in your functions.php file .
Your child theme functions.php should contain the below given code:
<?php add_action( 'wp_enqueue_scripts', 'photomania_pro_child_style' ); function photomania_pro_child_style() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); wp_enqueue_style( 'child-style',get_stylesheet_directory_uri() . '/style.css',array('parent-style')); wp_enqueue_script( 'photomania-child-custom-js', get_stylesheet_directory_uri() . '/custom.js', array('jquery'), '' , true ); }
Now create the custom.js file in your child theme and add below given code in the file .
jQuery(document).ready(function($){ $("a[href^=http]").each(function(){ if(this.href.indexOf(location.hostname) == -1) { $(this).attr({ target: "_blank", title: "Opens in a new window" }); } }) });
Hope this clears up the confusion.
Best regards!!
March 29, 2017 at 5:50 am #37246EZiserParticipantHi WEN,
You stated, “you have to enqueue the js script in your functions.php file .” I don’t know what this means. Could you clarify?
I have a functions.php file in my child theme. I added your second script; it did not work.
Let me know if I’m missing something. I’m versed in html, but novice in js. I’ll require step by step instruction.
You may email me directly if easier.
Thanks!
March 29, 2017 at 6:29 am #37247wensolutionsKeymasterHello @eziser,
Please follow below given Steps.
– First of all in your child theme functions.php file include below given code .
add_action( 'wp_enqueue_scripts', 'photomania_pro_child_style' ); function photomania_pro_child_style() { wp_enqueue_script( 'photomania-child-custom-js', get_stylesheet_directory_uri() . '/custom.js', array('jquery'), '' , true ); }
– Then create custom.js file in your child theme .
– Now add below given code in your child themes custom.js file .jQuery(document).ready(function($){ $("a[href^=http]").each(function(){ if(this.href.indexOf(location.hostname) == -1) { $(this).attr({ target: "_blank", title: "Opens in a new window" }); } }) });
Hope this helps.
If you have any confusion further, please let us know.
Best regards!!
March 29, 2017 at 7:19 am #37298EZiserParticipant“Then create custom.js file in your child theme .”
How do I do this?
March 29, 2017 at 7:46 am #37311wensolutionsKeymasterHello @eziser,
Steps to follow:
-First of all create the text document naming custom.txt and paste the js code with the help of text editor like notepad inside your child theme folder.
-Then rename the text file as custom.js and save it.
If you are finding it difficult to create the file replace above given code with code given below in your child themes functions.php file .
function photomania_pro_child_custom_js() { ?> <script type="text/javascript"> jQuery(document).ready(function($){ $("a[href^=http]").each(function(){ if(this.href.indexOf(location.hostname) == -1) { $(this).attr({ target: "_blank", title: "Opens in a new window" }); } }) }); </script> <?php } add_action( 'wp_footer', 'photomania_pro_child_custom_js' );
Hope this clears the confusion.
Best regards!!
March 29, 2017 at 8:15 am #37326EZiserParticipantHi WEN,
Sorry for the confusion – but where in the WordPress > Editor > Child theme is an option to “create file”. I can create it on my desktop, but there’s also nowhere to “upload file”.
I can provide you a support login if that would be easier. But if we continue here, I’ll need a step by step guide on how to accomplish this.
Thank you 🙂
LizMarch 29, 2017 at 9:12 am #37331EZiserParticipantSecondly, I added the first code to the function php. And it just inserted it as plain text atop every page.
I’m very confused – what can we do to streamline this process?
March 30, 2017 at 12:15 am #37344wensolutionsKeymasterHello @eziser,
Please check the mail for your follow up reply.
And if you have any theme related queries and need any help further feel free to post them .
Best Regards!!
March 30, 2017 at 12:38 am #37349EZiserParticipantGreat, thanks! We’ll continue the conversation there.
You’ve been a wonderful help. 🙂
March 7, 2020 at 11:04 pm #117925stiver1ParticipantHello
I want the same way
1 – Put automatic external link
2 – I also want to apply them to internal linksPossible way to my site because I tried it my site link
https://www.kora.news/It did not work
Please helpMarch 10, 2020 at 4:24 am #118064wensolutionsKeymasterHello,
The feature you are requesting is not possible.
Regarding your query to open external links, you can refer to the above conversation and add the code.
If the issue persists, let us know.
Thank you.
March 10, 2020 at 4:35 am #118065stiver1ParticipantI added the code and it didn’t work
Possible, what is the function of the code? Can a photo explain if my friend can
-
AuthorPosts
- You must be logged in to reply to this topic.