Forum Replies Created

Viewing 15 replies - 16 through 30 (of 74 total)
  • Thread Starter ace0930

    (@ace0930)

    @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;
    }
    
    Thread Starter ace0930

    (@ace0930)

    @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.

    Thread Starter ace0930

    (@ace0930)

    @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'] causes wp_safe_redirect () not to work? Is this a bug?

    Thread Starter ace0930

    (@ace0930)

    @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 works

    Thread Starter ace0930

    (@ace0930)

    @bcworkz I actually have the exit after the wp_safe_redirect (), but it’s not working…

    Thread Starter ace0930

    (@ace0930)

    @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 sprintf to make placeholders and replace them with arguments? Very weird to me…

    Thread Starter ace0930

    (@ace0930)

    @bcworkz It doesn’t make sense for me to escape the $path parameter of get_site_url because 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 return https://example.com/apple
    So the apple is definitely safe because I hardcode/manually type it. But how do I test the main/base_url part https://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!

    Thread Starter ace0930

    (@ace0930)

    Ok, I didn’t release the double quotes. Only Variable are parsed within it but not Constant.

    Thread Starter ace0930

    (@ace0930)

    @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 with esc_url. If I pass something like get_site_url ( null, test test ) and it returns https://test.com/test test, is that mean I need to use esc_url in 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 $path parameter? ( 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?

    Thread Starter ace0930

    (@ace0930)

    @hellofromtonya Appreciate your guideline! Thank you so much

    Thread Starter ace0930

    (@ace0930)

    @diondesigns Thanks.

    Thread Starter ace0930

    (@ace0930)

    @threadi Sorry because I misread plugin_basename( $file ) as basename(__FILE__)

    But I think you still misunderstood my point, I’m not comparing plugin_basename( $file ) with basename(__DIR__) . '/' . basename(__FILE__), of course the former is shorter.

    I’m looking at what plugin_basename does and it’s so much longer than my code basename(__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? LOL

    Thread Starter ace0930

    (@ace0930)

    @threadi I’ll not consider the original method register_activation_hook because it uses the plugin_basename which 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?

    Thread Starter ace0930

    (@ace0930)

    @threadi You should look at the documentation ( https://developer.www.ads-software.com/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.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Thanks for your patience and detailed answers! Really appreciate that!

Viewing 15 replies - 16 through 30 (of 74 total)