Add Target Attribute to the Link tag
-
How can I modify the link to open a new window instead of on the same page? Thanks.
-
Hi kyao,
I addressed this question in another post a little while back.
All you would need to do is create a child theme for whatever theme you are using and add the following lines to your functions.php file:
function dg_target_blank(){ ?> jQuery.ready( function(){ jQuery('.document-icon > a').attr('target', '_blank'); } <?php } add_action( 'wp_print_footer_scripts', 'dg_target_blank' );
At this time I do not want to add this functionality to the shortcode, though some changes are in the work which, when I have time to complete them, will drastically change a number of current functionalities, including allowing you to open documents in different locations.
-Dan
I already have a Child Theme functions.php file with various filters and actions for another Plugin.
I tried adding the code above and it broke the entire site because of the
<?php }How would I add the function and action above to a file that already has an opening tag
<?phpYes, I am a complete PHP ignoramus, so simple instructions much appreciated!
ThanksPHP is designed to be able to be started and stopped throughout a given file, so the syntax as written should be fine as long as you start it within an existing PHP starting tag (
<?php
).If you really wish to remove the start and stop of PHP, it gets uglier…
function dg_target_blank(){ echo 'jQuery.ready( function(){'; echo ' jQuery(".document-icon > a").attr("target", "_blank");'; echo '}'; } add_action( 'wp_print_footer_scripts', 'dg_target_blank' );
NOTICE: I am moderately drunk as of writing this, so take with a grain of salt.
I tried the code above!
It did not work, the PDF documents still open in the same window.
Plus it adds the following code to the bottom of each page
jQuery.ready( function(){ jQuery(“.document-icon > a”).attr(“target”, “_blank”);}
Sorry,
It should be this:
function dg_target_blank(){ ?> <script type="text/javascript"> jQuery.ready( function(){ jQuery('.document-icon > a').attr('target', '_blank'); } </script> <?php } add_action( 'wp_print_footer_scripts', 'dg_target_blank' );
Or this:
function dg_target_blank(){ echo '<script type="text/javascript">'; echo ' jQuery.ready( function(){'; echo ' jQuery(".document-icon > a").attr("target", "_blank");'; echo ' }'; echo '</script>'; } add_action( 'wp_print_footer_scripts', 'dg_target_blank' );
No, neither worked. I tried different browsers.
Plus had syntax error in first option for the line with closing
</script>
before opening<?php
At least the piece of code has gone from bottom of each page!
Sorry about that!
Finally actually tried the code myself. I was botching my jQuery syntax… This is tested and works:
function dg_target_blank(){ ?> <script type="text/javascript"> jQuery(document).ready( function(){ jQuery('.document-icon > a').attr('target', '_blank'); } ); </script> <?php } add_action( 'wp_print_footer_scripts', 'dg_target_blank' );
You can also echo each line and remove the opening/closing PHP tags if you like.
-Dan
I added the code and still no luck. I tried different browsers.
Now I am wondering if the problem is a conflict with another plugin and/or the fact the page requires prior login to view. Only other active plugin is WP-Members (Chad Butler).
You are welcome to take a look. Site is still under construction
https://newportbayclub.com/login/
login: member2
pswd: 123456It it helps, here is the code in the child theme functions . php
<?php /** WP Members Plugin part of redirect once logged in */ add_filter( 'wpmem_login_form', 'my_login_form_filter' ); function my_login_form_filter( $form ) { global $wpmem_a; if( $wpmem_a != 'pwdreset' && $wpmem_a != 'pwdchange' ) { return ''; } return $form; } /** * WP Members Plugin Block menus based on login and user role using wp_nav_menu_args * * This is close to cut-and-paste-code. You will need to make * some changes to reflect the menu names and possibly role * capabilities. To use/test as-is, you'll need to match up * menu names (value of $args['menu'] with the various (3) states: * * logged in, editor or higher role * * logged in, lower than editor role * * logged out */ add_filter( 'wp_nav_menu_args', 'wpmem_nav_menu' ); function wpmem_nav_menu( $args ) { if( is_user_logged_in() ) { /** the user is logged in, display menu based on role */ if( current_user_can( 'edit_posts' ) ) { // if the user has editor role or higher $args['menu'] = 'editor-menu'; // editor menu name } else { // user is logged in, but lower than editor role $args['menu'] = 'subscriber-menu'; // loer than editor menu } } else { /** * here the user is not logged in. display the * non-logged in menu */ $args['menu'] = 'logged-out'; // logged out menu name } return $args; } /** * WP-Members sidebar status filter example * * This is an example of the wpmem_sidebar_status * filter hook. The hook brings in the html $string * generated by the plugin and makes somes changes. */ add_filter( 'wpmem_sidebar_status', 'my_sidebar_status' ); function my_sidebar_status( $string ) { /** get the user info in an object using get_currentuserinfo */ global $current_user; get_currentuserinfo(); /** * use str_replace to find the original "You are logged in as $user_login" * and replace it with "Welcome, Firstname Lastname" and make * it <b>bold</b>. (NOTE: we are making the assumption here * that first_name/last_name are being used as required * registration fields */ $replace = '<b>Welcome, ' . $current_user->first_name . ' ' . $current_user->last_name . '</b>'; $needle = 'You are logged in as ' . $current_user->user_login; $string = str_replace( $needle, $replace, $string ); /** * use str_replace to find the original "click here to logout" * and replace it with "Not Firstname?" */ $replace = 'Not ' . $current_user->first_name . '?'; $needle = 'click here to logout'; $string = str_replace( $needle, $replace, $string ); /** * find the user's avatar, size it at 32px * attach it at the front of the html $string */ $string = '<div style="padding-top:4px;padding-right:4px;float:left;">' . get_avatar( $current_user->ID, '32' ) . '</div>' . $string; /** return the filtered html */ return $string; } add_action( 'wp_head', 'check_page' ); function check_page() { /** * If the page is the landing page AND the user is * logged in, then use wp_redirect to send the user * to some URL (presumeably the main site). */ if( is_page( 'login' ) && is_user_logged_in() ) { wp_redirect( 'https://newportbayclub.com/login/nbc-documents/' ); exit(); } return; } /** * Add a custom column to the Users > All Users table * showing the date the user registered */ add_filter( 'manage_users_columns', 'my_custom_user_columns', 99, 1 ); function my_custom_user_columns( $columns ) { // add my custom columns $columns['user_registered'] = 'Registered'; // return columns return $columns; } /** * gets the data for the custom column for each user. */ add_filter('manage_users_custom_column','get_my_custom_columns',10,3); function get_my_custom_columns( $value, $column_name, $user_id ) { // if this is the Registered column if ( 'user_registered' == $column_name ) { // get the user data as an object $user = get_userdata( $user_id ); // format date as mm/dd/yyyy return date( "m/d/Y", strtotime( $user->user_registered ) ); } return $value; } /** * Make the "Registered" custom column sortable */ add_filter('manage_users_sortable_columns', 'my_custom_column_sortable'); function my_custom_column_sortable( $columns ) { $custom = array ( 'user_registered' => 'user_registered' ); return wp_parse_args( $custom, $columns ); } /** * Sort the "Registered" custom column */ add_filter( 'request', 'my_custom_column_orderby' ); function my_custom_column_orderby( $vars ) { if( isset( $vars['user_registered'] ) && 'status' == $vars['user_registered'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'user_registered', 'orderby' => 'user_registered' ) ); } return $vars; } add_action('admin_head', 'remove_color_options'); function remove_color_options() { global $_wp_admin_css_colors; $_wp_admin_css_colors = 0; } add_action('admin_head','remove_url_bio'); function remove_url_bio(){ echo "\n" . '<script type="text/javascript">jQuery(document).ready(function($) { $(\'form#your-profile label[for=url], form#your-profile input#url\').hide(); $(\'form#your-profile label[for=description], form#your-profile textarea#description, form#your-profile span.description\').hide(); }); </script>' . "\n"; } /** * Removes unused contact info fields * * You don't have to remove them all. If you want to keep * one or two of the defaults, just remove the line(s) that * do/don't apply. How remove twitter, facebook, google profile, about page */ add_filter( 'user_contactmethods', 'remove_contact_fields', 10, 1 ); function remove_contact_fields( $contactmethods ) { unset( $contactmethods['aim'] ); unset( $contactmethods['jabber'] ); unset( $contactmethods['yim'] ); unset( $contactmethods['twitter'] ); unset( $contactmethods['facebook'] ); return $contactmethods; } /** * Redirect to /nbc-documents/ when a user logs in */ add_filter( 'wpmem_login_redirect', 'my_login_redirect' ); function my_login_redirect() { return 'https://newportbayclub.com/login/nbc-documents/'; } /** * Removes the extra social fields that Headway Theme adds to the user profile */ add_action( 'after_setup_theme', 'remove_headway_social_fields' ); function remove_headway_social_fields() { remove_filter('user_contactmethods', array('HeadwaySocialOptimization', 'add_headway_contact_methods')); } /** * Document Gallery Plugin open file in new window */ function dg_target_blank(){ ?> <script type="text/javascript"> jQuery(document).ready( function(){ jQuery('.document-icon > a').attr('target', '_blank'); } ); </script> <?php } add_action( 'wp_print_footer_scripts', 'dg_target_blank' );
Just looked at the site you linked to and the issue is that you don’t have jQuery loaded anywhere. WordPress includes jQuery so something custom you’re doing is removing that. If you fix that, the code I gave you should work.
Dan, thanks for pin pointing the problem. I have added plugin WP jQuery Plus and now the documents open in a new window.
Thanks again.
No problem, Toni.
I would look into why jQuery is missing. A plugin/theme you’re running is removing it and it shouldn’t. You shouldn’t need a 3rd-party plugin to get jQuery back.
All the best!
-DanI have a similar question, but I want to open some text in a sidebar. I have an imagemap of our state with the different cities where our schools are located. I have been able to click on the town and the city name pops up, but I’d like it to open in a sidebar with the project summary. This is how I want people to see our previous years’ projects. I am brand new at this so any help is appreciated. Thanks.
- The topic ‘Add Target Attribute to the Link tag’ is closed to new replies.