How to convert this php code in a wordpress shortcode? - php

Hi I found this code on online, it is a simple button to reveal the coupon code and open the URL at the same time.
function coupon_add($coup, $uurl)
{
echo "
<script type='text/javascript'>
<!--
function coupon(coup,url)
{alert('COUPON CODE: ' + coup);
window.open(url,'_blank');
}
//-->
</script>";
echo "<center><br /><input style=\"width:250px; height:60px;font-
size:30px;\" type=\"button\" onclick=\"coupon('".$coup."','".$uurl."')\"
value=\"VIEW COUPON\" \/></center><p>";
}
I would like to turn this into a short code so I can insert it into a post. Would love some help with this, thanks in advance!

In your functions.php:
add_shortcode( 'my_coupon_add', 'sc_my_coupon_add' );
function sc_my_coupon_add( $args, $content = null ){
$atts = shortcode_atts( array(
'coup' => '',
'uurl' => ''
), $args, 'my_coupon_add' );
return "<script type='text/javascript'>
function coupon( coup, url ){
alert( 'COUPON CODE: ' + coup );
window.open( url, '_blank' );
}
</script>
<center><br /><input style=\"width:250px; height:60px;font-size:30px;\" type=\"button\" onclick=\"coupon('" . $atts[ 'coup' ] . "','" . $atts[ 'uurl' ] . "')\" value=\"VIEW COUPON\" \/></center><p>";
}
Now you can use the shortcode [my_coupon_add coup="" uurl=""] and pass your parameters. The way the javascript is written, this can only be used once on a page or post or else you'll have issues with the coupon() function, but this should give you enough to scale/rewrite it if needed more than once (like moving the coupon() javascript function to a main js file so it's available once and only when needed).
You might need to double-check the single and double quotes - I whipped this up quickly.

Related

WordPress/Woocommerce Inline Javascript adding Nonce and CSP not working as expected

I have been trying to add a Nonce to inline JS in Wordpress primarily focusing on WooCommerce. So far, I've managed to add the Nonce to registered JS but the inline JS code generated out of plugins such as Woocommerce isn't getting the Nonce added.
The approach which has worked for registered JS is...
add_filter( 'script_loader_tag', 'add_nonce_to_script2', 10, 3 );
function add_nonce_to_script2( $tag, $handle, $src ) {
global $my_nonce2;
$my_nonce2 = wp_create_nonce('my__script__nonce');
return '<script type="text/javascript" src="' . esc_url( $src ) . '" nonce="nonce-' . esc_attr( $my_nonce2 ) . '"></script>';
}
The approach which isn't working, maybe because I don't know the right $var for data to pass to the below solution from Woocommerce, any guidance would be appreciated.
Example code I'm trying to target.
<script type='text/javascript'>
/* <![CDATA[ */
var wc_add_to_cart_params = {"ajax_url":"\/wp-admin\/admin-ajax.php","wc_ajax_url":"\/?wc-ajax=%%endpoint%%","i18n_view_cart":"View cart","cart_url":"https:\/\/mysite.com\/cart\/","is_cart":"","cart_redirect_after_add":"no"};
/* ]]> */
</script>
Solution that isn't working below...
add_filter( 'script_loader_tag', 'add_nonce_to_script_tag', 10, 3 );
function add_nonce_to_script_tag( $tag, $handle, $src ) {
// Check the $handle and respond accordingly
if ( $handle === 'my-script' ) {
$nonce_value = wp_create_nonce('my__script__nonce'); // or ref to an existing nonce
$replace = sprintf("javascript' nonce='%s'>", $nonce_value );
$tag = str_replace( "javascript'>", $replace, $tag);
}
return $tag;
}
// Then... $data is the inline JS from wherever
wp_add_inline_script('my-script', $data, 'before');

Print all en-queued scripts/styles

I am trying to add async attribute in my rander js and css script .
for these I amusing this script for print en-queued script .but this code is
only print some admin js not more.I am using this code from git hub and other stack overflow solution , but not able to print all script .
<?php
/*
* Getting script tags
* Thanks http://wordpress.stackexchange.com/questions/54064/how-do-i-get-the-handle-for-all-enqueued-scripts
*/
add_action( 'wp_print_scripts', 'wsds_detect_enqueued_scripts' );
function wsds_detect_enqueued_scripts() {
global $wp_scripts;
foreach( $wp_scripts->queue as $handle ) :
echo $handle . ' | ';
endforeach;
}
?>
If you wanna see all the registered or enqueued scripts then try the following code. $wp_scripts->queue will only show those scripts that are going to be printed or added to site.
add_action( 'wp_print_scripts', 'wsds_detect_enqueued_scripts' );
function wsds_detect_enqueued_scripts() {
global $wp_scripts;
foreach( $wp_scripts->registered as $script ) {
echo $script->handle . ' | ';
}
}

How to return the translated string in a custom translation shortcode in WordPress

I have a translation plugin (Loco Translate), and I want to translate my page using <?php _e('text', 'domain'); ?> in my page editor in the wordpress administration. Is it possible ?
I tried shortcodes but the result is commented ... Thanks !
EDIT :
My shortcode function :
function translation_shortcode($atts) {
$a = shortcode_atts( array(
'name' => '',
), $atts );
return "<?php echo __('" . $a['name'] . "', 'pulsdesign'); ?>";
}
add_shortcode( 'translation', 'translation_shortcode' );
In the page editor :
[translation name="Softwares and sensors for remote and real time physiological monitoring"]
The result :
<!--?php echo __('Softwares and sensors for remote and real time physiological monitoring', 'pulsdesign'); ?-->
The problem appears to be return "<?php echo __('" . $a['name'] . "', 'pulsdesign'); ?>";
This will return the string <?php ..., which will end up in the browser as a whole. And browsers don't know what to do with php code, they can't execute it.
Instead you should return the result of the translation, as Marcel Kohls commented:
return __($a['name'] 'pulsdesign');

How to handle user_row_actions in WordPress?

First of all, I have not find solution for my problem. I have read few articles and thread to create a user actions , and tried the following code.
function kv_admin_deactivate_link($actions, $user_object) {
$actions['deactivate_user'] = "<a href='" . admin_url( "users.php?action=deactivate&user=$user_object->ID") . "'>" . __( 'Deactivate', 'kvc' ) . "</a>";
return $actions;
}
add_filter('user_row_actions', 'kv_admin_deactivate_link', 10, 2);
After using the above code it gets me the additional action with users list as like in the below screenshot.
Now, my problem is, I don't know how to proceed to write my codes to deactivate a user. So can you help me to write function to handle /wp-admin/users.php?action=deactivate&user=41. Here I am writing these function for my wordpress theme and how can I write function for it.
This will help you to perform your deactivate operation function. just create a admin menu with the following code.
function xxxadmin_submenu_email() {
add_menu_page('Your menu', 'Your menu', 'manage_options', 'deactivate' , 'xxx_deactivate_functions', '', 66);
}
add_action('admin_menu', 'xxxadmin_submenu_email');
Now, it will get you a page and you have to write function inside xxx_deactivate_functions it. Here is sample code.
function xxx_deactivate_functions() {
if(isset($_GET['action']) && $_GET['action']== 'deactivate'){
$user_id = $_GET['user'];
$user_info = get_userdata($user_id);
}
}
And I have modified your function like the below one.
function kv_admin_deactivate_link($actions, $user_object) {
$actions['deactivate_user'] = "<a href='" . admin_url( "users.php?page=deactivate&action=deactivate&user=$user_object->ID") . "'>" . __( 'Deactivate', 'kvc' ) . "</a>";
return $actions;
}
add_filter('user_row_actions', 'kv_admin_deactivate_link', 10, 2);
This is one way to do this. But wait for some other experts solution to code it better way. Hope its helpful for urgent need.

Cannot modify header information - headers already sent in WordPress [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
When I go into wp-admin on www.newswars.org, I see the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/newswars/public_html/wp-content/themes/videoplus/functions.php:38) in /home/newswars/public_html/wp-includes/option.php on line 563
Warning: Cannot modify header information - headers already sent by (output started at /home/newswars/public_html/wp-content/themes/videoplus/functions.php:38) in /home/newswars/public_html/wp-includes/option.php on line 564
Can you please help?
EDIT: Ok its this that is causing me the problem.
$user_ej = wp_get_current_user();
if ($user_ej->roles[0] == 'contributor')
{ ?>
This is 36-38 lines. For some reason its getting all messed up. Still I'm not quite sure what the problem is. I tried removing ?> but that gave me a syntax error. I'm at a loss. It is definitely the problem area as it only comes up when someone logs in a 'contributor'.
<?php
// Translations can be filed in the /lang/ directory
load_theme_textdomain( 'themejunkie', TEMPLATEPATH . '/lang' );
require_once(TEMPLATEPATH . '/includes/sidebar-init.php');
require_once(TEMPLATEPATH . '/includes/custom-functions.php');
require_once(TEMPLATEPATH . '/includes/post-thumbnails.php');
require_once(TEMPLATEPATH . '/includes/theme-postmeta.php');
require_once(TEMPLATEPATH . '/includes/theme-options.php');
require_once(TEMPLATEPATH . '/includes/theme-widgets.php');
require_once(TEMPLATEPATH . '/functions/theme_functions.php');
require_once(TEMPLATEPATH . '/functions/admin_functions.php');
function wpr_snap($atts, $content = null) {
extract(shortcode_atts(array(
"snap" => 'http://s.wordpress.com/mshots/v1/',
"url" => 'http://www.catswhocode.com',
"alt" => 'My image',
"w" => '400', // width
"h" => '300' // height
), $atts));
$img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>';
return $img;
}
add_shortcode("snap", "wpr_snap");
$user_ej = wp_get_current_user();
if ($user_ej->roles[0] == 'contributor')
{ ?>
<style type="text/css">
#menu-dashboard, #toplevel_page_wpcf7, #menu-tools
{
display:none;
}
</style>
<?php }
add_filter( 'gettext', 'change_post_to_portfolio' );
add_filter( 'ngettext', 'change_post_to_portfolio' );
function change_post_to_portfolio( $translated ) {
$translated = str_ireplace( 'Posts', 'VIDEOS', $translated ); // ireplace is PHP5 only
return $translated;
}
// Uncomment this to test your localization, make sure to enter the right language code.
// function test_localization( $locale ) {
// return "nl_NL";
// }
// add_filter('locale','test_localization');
// Adds categories to pages
add_action('admin_init', 'reg_tax');
function reg_tax() {
register_taxonomy_for_object_type('category', 'page');
add_post_type_support('page', 'category');
}
add_action('admin_footer', 'my_admin_footer');
function my_admin_footer()
{
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL ;
$message = NULL;
if ($uri AND strpos($uri,'edit.php'))
{
if (strpos($uri,'post_type=page'))
{
$message = '1.In the ‘Video Embed Code section,’ enter the video embed code. 2.Enter the title and text in the main panel below. 3.Choose which continent and category is most fitting for your video. 4.Press ‘Publish’';
}
else
{
$message = 'ALL VIDEOS';
}
}
elseif ($uri AND strpos($uri,'post-new.php'))
{
if (strpos($uri,'post_type=page'))
{
$message = 'Add pages here';
}
else
{
$message = '1.In the ‘Video Embed Code section,’ enter the video embed code. 2.Enter the title and text in the main panel below. 3.Choose which continent and category is most fitting for your video. 4.Press ‘Publish’';
}
}
elseif ($uri AND strpos($uri,'post.php'))
{
$message = 'THREE';
}
if ($message)
{
?><script>
jQuery(function($)
{
$('<div style="margin-bottom:15px; color:#FF0000;"></div>').text('<?php echo $message; ?>').insertAfter('#wpbody-content .wrap h2:eq(0)');
});
</script><?php
}
}
?>
add_shortcode("snap", "wpr_snap");
$user_ej = wp_get_current_user();
if ($user_ej->roles[0] == 'contributor')
{ ?>
<style type="text/css">
#menu-dashboard, #toplevel_page_wpcf7, #menu-tools
{
display:none;
}
</style>
<?php }
add_filter( 'gettext', 'change_post_to_portfolio' );
add_filter( 'ngettext', 'change_post_to_portfolio' );
isn't inside a function. So it's being called as soon as the file loads. This means all output is sent to the screen immediately This stuff should be inside a function which is then called at the right time (like the other bits).

Categories