Forum Replies Created

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

    (@ace0930)

    @greenshady Hi, I’m trying to use the hook pre_set_site_transient_update_themes to receive update ( for theme ) from my own API. I’m following the turtorial here, but I saw others articles has this line of code if (empty($transient->checked)) { return $transient; } which I’m not sure what it does and if I need it. Thank you!

    Thread Starter ace0930

    (@ace0930)

    Update: Use offsetX and offsetY instead.

    Thread Starter ace0930

    (@ace0930)

    @hirenbhanderi Exactly, but how does WordPress get the base URL localhost/example before putting it into the wp_option table, that’s what I’m asking…

    As I have mentioned that the $_SERVER['SERVER_NAME'] only returns localhost, so I wonder how WordPress finds localhost/example.

    Thread Starter ace0930

    (@ace0930)

    @hirenbhanderi Hi, get_site_url() is also using get_option ( 'siteurl' ) if you look at the source code.

    I’m not asking how to get the base URL like which function to use, I’m asking how WordPress gets the siteurl…Haha

    Thread Starter ace0930

    (@ace0930)

    @threadi You saved my day, thank you so much!

    Thread Starter ace0930

    (@ace0930)

    @threadi To be honest, I still do not fully understand. But do you mean the function handle_data () will not be include/require on the form page? If it’s true, then my question is solved.

    Thread Starter ace0930

    (@ace0930)

    @threadi From the doc

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

    Normally, we will have another file/script to handle the form like, right?
    Assume index.php has a form and it looks like this:

    
    <form action="process.php" method="post"></form>
    

    Then in the process.php file, we use $_POST to get the data like this ( call this “example-1” for later reference ):

    
    $name = $_POST['name'];
    
    header ( "Location: https://example.com" ); // Redirect user to https://example.com after processing the data.
    

    But if we put the processing code above on the same file ( index.php ) and it looks like this ( call this “example-2” for later reference ):

    
    <form action="process.php" method="post"></form>
    
    <?php
    $name = $_POST['name'];
    
    header ( "Location: https://example.com" );
    ?>
    

    It would cause an error because there is already an output ( the form ) before the header ().

    Now let’s get back to the “WordPress” way of handling form, we will use the hook admin_post_{$action} with a function like:

    
    add_action( admin_post_handle_data, handle_data );
    

    Then we write the processing code inside handle_data like the below:

    
    function handle_data () {
       $name = $_POST['name'];
    
       header ( "Location: https://example.com" );
    }
    

    The form page is created by add_menu_page with the admin_menu hook.

    My question is:
    1) Will this function handle_data () be include/require on the form page? If it does, it will look like the “example-2”.

    2) If the question “1)” is a yes, then the sequence matter, whether it’s admin_post first or admin_menu first. If it’s admin_post first, then the function handle_data () is before the form; If it’s the admin_menu first, then the function handle_data () is after the form.

    Or am I complicating it, WordPress will just do the traditional way like “example-1” where the processing code is in another script/file?

    • This reply was modified 2 years, 4 months ago by ace0930.
    Thread Starter ace0930

    (@ace0930)

    @threadi Hi, sorry but I don’t think you understand the question…

    Thread Starter ace0930

    (@ace0930)

    @bcworkz It returns NULL when I echo prepare () if I %s the table name.

    And I found this line in the source code:
    $query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s.

    I believe it becomes 'table_name' ( note the quotes ) if %s the table name, and 'table_name' is different from table_name.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Nonono, you didn’t get the point. I didn’t put the wp_safe_redirect on the first line of the page like:

    
    <?php wp_safe_redirect ( $example ); ?>
    
    <form></form>
    

    Instead, it’s in a function that would be called once the form is submitted to the admin-post.php with this hook. And it looks like this:

    
    <?php function send () { wp_safe_redirect ( $example ); } ?>
    
    <form></form>
    

    Which mean the wp_safe_redirect ( $example ) will not trigger until the function is called.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Is that mean the $table_name shouldn’t be replace by %slike the below:
    $wpdb -> prepare ( "SELECT !value! FROM !$table_name! WHERE !name! = %s", $name )

    Instead and although the below matches the principle of sprintf but it won’t work with $wpdb -> prepare:
    $wpdb -> prepare ( "SELECT !value! FROM %s WHERE !name! = %s", array ( $table_name, $name ) )

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Nope, it seems you misunderstood my point. It’s my fault not to mention that the redirect code is in a function that will be triggered after the form is submitted using this method. So the redirect code is not on the page itself…

    Thank you so much for your advice!

    Thread Starter ace0930

    (@ace0930)

    @bcworkz Okay, cannot believe this mistake took me two days, I think I know why.

    Because the action of the form is set to admin-post.php, of course, the current URL would always be that…LOL

    But you may still explain why $_SERVER['REQUEST_URI'] may cause so many redirect error.

    Thread Starter ace0930

    (@ace0930)

    @bcworkz I found a relevant question although I don’t understand what the answer is talking about: https://wordpress.stackexchange.com/questions/25764/cant-pass-table-to-wpdb-prepare

    quotation marks are added to the table name

    So $table_name = $wpdb -> prefix . testing; will become "wp_testing" according to the answer? Why is that so?

    And you said:

    Even though including a variable in the first string arg works, it’s incorrect. All vars should be passed in the second arg’s array.

    ??

    Thread Starter ace0930

    (@ace0930)

    @bcworkz I have also tried:

    
    $url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];

    It redirects to: https://example.com/wp-admin/admin-post.php?.

    With the original code:
    https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']

    It always redirects to https://example.com/wp-admin/admin-post.php

Viewing 15 replies - 1 through 15 (of 74 total)