How to register in Wordpress database from Advanced custom field

Asked

Viewed 163 times

0

On my site you can insert new photo albums from the ACF. I created a table in the database that contains the album information, such as name, location, date, etc. Only I have no idea how to insert this album information into the database.

I think the code of insertion is like this, but I don’t know how to relate it to the ACF.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bd";


$conn = new mysqli($servername, $username, $password,$dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Conectado!";


$nome_album = $_POST['nome_album']; //Seria o nome que o usuário adiciona no form do ACF
$local_album = $_POST['local_album'];
$data_album = $_POST['data_album'];


$query = "INSERT INTO albums (nome_album, local_album, data_album)
VALUES ('$nome_album', '$local_album', '$data_album')";

if ($conn->query($query) === TRUE) {
    echo "cadastro realizado com sucesso";
} else {
    echo "Error: " . $query . "<br>" . $conn->error;
}

$conn->close();
?>

1 answer

0

Look, it is not recommended you manipulate the Wordpress database this way, if you will use the ACF to create fields is ideal to do everything for it. Looking at your scenario I would recommend:

  1. Create a Custom Post Type called ALBUNS. You can do it using the Custom Post Type UI plugin - https://br.wordpress.org/plugins/custom-post-type-ui/

Here you have two options:

2.1. Create custom Fields using ACF associating to CUSTOM POST TYPE created.

2.2. Create custom taxonomies called LOCAL and DATA (the advantage here in my opinion is that it is easier to make the WP standard search system see). You can also do it using the Custom Post Type UI plugin - https://br.wordpress.org/plugins/custom-post-type-ui/

This way you protect your database and use WP’s own resources to organize the data you need.

  • Thank you @Felipe, I did not know this plugin Custom Post Type, I will take a look at the documentation of it. Thanks even for the tip!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.