get post Id in plugin

Capslock10

I am making a plugin in wordpress. And I am trying to get the post id when I click the publish button on the add new post. And now, I get an internal error(500) when I use the get post function.

I am using_POST['post'] now, but how can I use the wordpress function to get the post id?

Here is my code:

//require the php
require_once( FACEBOOK_API_PLUGIN_DIR . 'js/databaseConnection.php' );

Code on databaseConnection.php:

function get_post()
    {
        global $wp_query;
        $thePostID = $wp_query->post->ID;
        return $thePostID;
    }


function try_insert($post_id)
{
    $test02 = 333243;
    $test03 = 222;
     $link = @mysqli_connect(
                'localhost',
                'root',
                '',
                'wordpress'
          ) or die("connection failed");

             $sql = "INSERT INTO post_data02 (post_id, condition_code) VALUES ('$post_id','$test03')";

            if ($link->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $link -> error;
            }
     $link->close();
}
add_action( 'publish_post', 'try_insert', get_post());

Also, when I disable the get_post() function I error will be gone. What am I doing wrong?

thanks,

Jim

You don't need get_post at all and you are using the publish_post hook and add_action function incorrectly.

Additionally, have a look at the WPDB class. If your table is in the same database and schema as your WordPress tables you don't need to use mysqli_connect - WordPress has already connected to the database for you!

Note also that you should NEVER pass values into a string to be used as part of an SQL statement! This is a MASSIVE security risk! ALWAYS use prepared statements and parameters (WPDB provides this as well).

Try this:

function try_insert($post_id, $post)
{
    // Pull in the global WPDB variable WordPress creates
    global $wpdb;

    $test02 = 333243;
    $test03 = 222;

    /*
     * Insert record into the table "post_data02" with the values:
     *   "post_id" => The ID in $post_id passed by WordPress,
     *   "condition_code" => The number in $test03 (currently 222)
    */
    $insert = $wpdb->insert( 'post_data02',
        [ 'post_id' => $post_id, 'condition_code' => $test03 ],
        [ '%d',                  '%d' ]
    );

    if( $insert !== false ) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $link -> error;
    }

}
add_action( 'publish_post', 'try_insert', 10, 2);

Please re-read the links I provided above as they provide excellent examples and show you how to use the functions, methods, hooks, etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using $_GET and $_POST in custom WordPress plugin

From Dev

Wordpress get post id with matched post content

From Dev

How to get the post thumbnail with global post ID

From Dev

Get author ID of a post in wordpress

From Dev

Get author ID of a post in wordpress

From Dev

Get id of post with array in wordpress

From Dev

Get Wordpress data by Post ID

From Dev

Get one facebook post by ID

From Dev

select post where id is not get_the_ID()

From Dev

Get id and a field value in EasyAutocomplete plugin

From Dev

get_the_id vs. post->ID vs. the_id / get_post_meta

From Dev

Wordpress get post ID from Widget

From Dev

How to get post by tag_id of the category

From Dev

WordPress Shortcode - Get post data by ID

From Dev

How can i get the facebook post id?

From Dev

Get a post id from me/home

From Dev

Facebook API GET recent post ID

From Dev

Get the Page/Post ID of included Shortcode in WordPress

From Dev

WordPress Shortcode - Get post data by ID

From Dev

Validate object ID from GET to POST

From Dev

how to get the current id of a post while commenting to it

From Dev

Using $_POST or $_GET with Modal, Refresh based on ID

From Dev

How to get Category name by id post in wordpress

From Dev

recyclerview textview click get post_id?

From Dev

Wordpress - How to get the next new post ID

From Dev

Get Yoast Plugin infos on publish_post Wordpress

From Dev

How to get Last post Id of custom post type in wordpress

From Dev

How to get WordPress post featured image from post ID in url

From Dev

Get one post by post type and ID inside of a loop