ace0930
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: SQL – Single quotes and backticks for query@bcworkz Sorry but your code still doesn’t work 🙁
$wpdb -> prepare ( "SELECT !value! FROM %s WHERE !name! = %s", array ( $table_name, $name ) )And Your original code:
$wpdb -> prepare ( "SELECT !value! FROM %s WHERE !name!= '%s';", array( $table_name, $name ,) )Until I insert the
!$table_name!inside then it works. Why??????
$wpdb -> prepare ( "SELECT !value! FROM !$table_name! WHERE !name! = %s", $name )Below is the full code and you can try:
function get_data ( $name ) { global $wpdb; $table_name = $wpdb -> prefix . testing; $current_status = $wpdb -> get_var ( $wpdb -> prepare ( "SELECT !value! FROM %s WHERE !name! = %s", array ( $table_name, $name ) ) ); return $current_status; }Forum: Developing with WordPress
In reply to: Passing function into wp_safe_redirect@bcworkz I have created a menu page with a form, I want the user to go back to the menu page ( same place ) after submitting the form.
Sorry because I don’t understand how
$_SERVER['REQUEST_URI']will cause so many errors, can you elaborate more?Or actually, do you have a better alternative for this? My ideal result is sending the user back to the place where they submit the form. This cannot be hardcoded and it should be dynamic.
Forum: Developing with WordPress
In reply to: Passing function into wp_safe_redirect@bcworkz I have realized that it’s caused by the
$_SERVER['REQUEST_URI']part, as soon as it’s removed, it works.Why and how
$_SERVER['REQUEST_URI']causeswp_safe_redirect ()not to work? Is this a bug?Forum: Developing with WordPress
In reply to: SQL – Single quotes and backticks for query@bcworkz Not sure if this is a bug, the below code won’t work:
$current_status = $wpdb -> get_var ( $wpdb -> prepare ( "SELECT !value! FROM %s WHERE !name! = %s", $table_name, $name ) );But the below code work:
$current_status = $wpdb -> get_var ( $wpdb -> prepare ( "SELECT !value! FROM !$table_name! WHERE !name! = %s", $name ) );P.S, I use
!to replace the backticks here. Notice the backtick around$table_name, not sure if it is needed but it worksForum: Developing with WordPress
In reply to: Passing function into wp_safe_redirect@bcworkz I actually have the
exitafter thewp_safe_redirect (), but it’s not working…Forum: Developing with WordPress
In reply to: SQL – Single quotes and backticks for query@bcworkz You’re right and I should look at the doc 🙂
But the doc said: “Arguments may be passed as individual arguments to the method, or as a single array containing all arguments”
1) In your answer, there are two arguments, shouldn’t we use the array to contain the two arguments?
2) I store the table name in a constant, does it need with the backtick too? But how?define ( 'table_name', 'wp_table_name' ); $wpdb -> prepare ( "SELECT !value! FROM " . table_name . " WHERE !name! = %s", $name )3) Why use the sprintf()-like syntax? Variables in double quotes can be parsed within it in PHP already, why do we need
sprintfto make placeholders and replace them with arguments? Very weird to me…Forum: Developing with WordPress
In reply to: Which query need to be escaped@bcworkz It doesn’t make sense for me to escape the
$pathparameter ofget_site_urlbecause it’s me who wrote it and I know it’s safe. I was asking how to test the main/base_url part because I cannot add any data to it.For example,
get_site_url ( 'apple' )would returnhttps://example.com/apple
So theappleis definitely safe because I hardcode/manually type it. But how do I test the main/base_url parthttps://example.com/because I cannot put any data into it fro testing purposes…For the query part, I understand now and I agree with you, thank you so much!
Forum: Developing with WordPress
In reply to: Storing database table name as constOk, I didn’t release the double quotes. Only Variable are parsed within it but not Constant.
Forum: Developing with WordPress
In reply to: Which query need to be escaped@bcworkz Hi, why not just use the PHP prepared statement “link“, but use
$wpdb -> prepare?Sorry for running out a bit of topic, from the last question regarding
esc_url, you mentioned that I could have put in some data into the function and check if it need to be escaped withesc_url. If I pass something likeget_site_url ( null, test test )and it returnshttps://test.com/test test, is that mean I need to useesc_urlin this case since the space is not escaped? BUT is that a possibility that the function only escapes for the base URL part but not the$pathparameter? ( wish it sounds not complicated to you…LOL )Back to the topic, do you mean unlike testing the URL as I have done above, I have to look at the source code for the query this time yaa?
Forum: Developing with WordPress
In reply to: namespace not working correctly@hellofromtonya Appreciate your guideline! Thank you so much
Forum: Developing with WordPress
In reply to: namespace not working correctly@diondesigns Thanks.
Forum: Developing with WordPress
In reply to: Alternative of register_activation_hook@threadi Sorry because I misread
plugin_basename( $file )asbasename(__FILE__)…But I think you still misunderstood my point, I’m not comparing
plugin_basename( $file )withbasename(__DIR__) . '/' . basename(__FILE__), of course the former is shorter.I’m looking at what
plugin_basenamedoes and it’s so much longer than my codebasename(__DIR__) . '/' . basename(__FILE__)if you look at the source.Below is the full code of
plugin_basename:function plugin_basename( $file ) { global $wp_plugin_paths; // $wp_plugin_paths contains normalized paths. $file = wp_normalize_path( $file ); arsort( $wp_plugin_paths ); foreach ( $wp_plugin_paths as $dir => $realdir ) { if ( strpos( $file, $realdir ) === 0 ) { $file = $dir . substr( $file, strlen( $realdir ) ); } } $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); // Get relative path from plugins directory. $file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file ); $file = trim( $file, '/' ); return $file; }So comparing the above code with my code
basename(__DIR__) . '/' . basename(__FILE__), which one is shorter? LOLForum: Developing with WordPress
In reply to: Alternative of register_activation_hook@threadi I’ll not consider the original method
register_activation_hookbecause it uses theplugin_basenamewhich does a lot more than my/your suggestion ( if they work ).May I know why
'action_' . plugin_basename(__FILE__)works?plugin_basename(__FILE__)will return only the file name, right?WordPress expects to have the folder name too according to the documentation?
Do I misunderstood the doc or you are missing something?
Forum: Developing with WordPress
In reply to: Alternative of register_activation_hook@threadi You should look at the documentation ( https://developer.wordpress.org/reference/functions/register_activation_hook/ ).
The hook should be
activate_sampleplugin/sample.php, and ‘plugin_basename(__FILE__)’ will return only the file name ( sample.php ) without the folder name ( sampleplugin ).And that’s the question I was asking, why can’t I just use
add_action( 'activate_' . basename(__DIR__) . '/' . basename(__FILE__), 'my_callback_function' );instead of ‘register_activation_hook( __FILE__, ‘my_callback_function’ );’?I have explained in detail that my suggestion is better because it is shorter.
Forum: Developing with WordPress
In reply to: wp dropdown categories not showing selected option@bcworkz Thanks for your patience and detailed answers! Really appreciate that!