• Hi,

    I spend about two days looking for a code (I don’t want plugins) which we can add a custom url just to open in target blank. Something like: https://www.mysite.com/custom/123

    I’m not looking for a plugin which add ALL external links in new tab nor something like that, just a functions.php code or htacess.

    Also, I don’t want to do this manually for each post.

    Thanks in advance

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello,

    Before asking a question, please make sure the answer doesn’t already exist in the support forum :).

    custom menu items target

    Thread Starter kinkyamateur

    (@kinkyamateur)

    Hello,

    I’m not talking about MENU and the question doesn’t already exsits in the forum. Please, read well my question.

    Thanks

    Hello again,

    My bad for misunderstanding the question, could you try to explain a bit more? I still don’t see what you’re trying to do.

    Thread Starter kinkyamateur

    (@kinkyamateur)

    Thanks

    I have a lot of this links in my website: https://www.mysite.com/custom/123

    I don’t want to edit one by one to add the target blank because I have about hundreds.

    I need that that link it is opened in a new tab. But the link it is internal, not external.

    I want to avoid plugins, so that’s the reason I asked about some code or htacess trick.

    Thanks ??

    I see what you mean.

    if you have a common pattern like domain/custom, you can use javascript or jQuery to find all links with that pattern and add the target attribute.

    $('a[href*=domain/custom"]').attr('target','_blank');

    Thread Starter kinkyamateur

    (@kinkyamateur)

    Thanks for reply,

    If I use Javascript for add the attribute for the actual links, what will happens with the next ones? Must I use Java each time a link it is added to the website? I mean, I have an automatic blog so this doesn’t works for future post, just the posted. Does there is no trick in htacess? Each time a custom link is posted, automatically converts to target blank?

    Make a javascript file, put the code you need on it, upload it to your theme folder (to the js folder or wherever js scripts are), add it to the theme using wp_enqueue_style and it will run on all your site’s pages.
    Any new post or page will have the same behavior for the links.
    If you don’t manage to do it, I’ll give you some working code when I’m at my workstation ?? .

    Thread Starter kinkyamateur

    (@kinkyamateur)

    Thanks for reply, I have no idea how to make a javascript file and it scares me a bit but I’m gonna try.

    What I don’t understand it’s about wp_enqeue_style point.

    So, first of all I need to use this code (with my domain), right?

    $(‘a[href*=domain/custom”]’).attr(‘target’,’_blank’);

    Thanks for your help

    Sorry, I meant wp_enqueue_script.

    Go to your theme folder, if you don’t have a folder called js there, make it.
    create a new file in the js folder call it custom-target.js.

    put this code in the js file:
    $('a[href*=domain/custom"]').attr('target','_blank');

    change domain/custom accordingly to your need.

    add this code to your functions.php file in your theme:

    function wp48548_targets_js(){
    	wp_enqueue_script('custom_target_js',get_template_directory_uri().'/js/custom-target.js', array('jquery'),'',true);
    }
    add_action( 'wp_enqueue_scripts', 'wp48548_targets_js' );
    Thread Starter kinkyamateur

    (@kinkyamateur)

    I can’t believe it. It works fine!

    I appreciated so much your help. Thanks a lot! ??

    Glad I could help ??
    please mark it as resolved.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Custom URL in new tab without plugin’ is closed to new replies.