ACF & PHP-API (WordPress): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(14 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
 +
How can you automatically manage ACF fields? In particular, fill in, delete and edit?
  
== Zie ook ==
+
== Not directly at database level ==
  
* [[ACF (Advanced Custom Fields, WordPress)]]
+
To start with some bad news: While ACF fields are stored in tables <code>wp_options</code> or <code>wp_postmeta</code>, you cannot write directly to these tables to create ACF fields. Then the 'context' is missing, eg a repeater. So you have to do it via an API call.
  
== the_field() ==
+
== Two kinds of fields ==
  
Voorbeeld:
+
There seems to be a distinction between two kinds of ACF fields and how to approach them:
 +
 
 +
* Fields that are associated with specific posts
 +
* Fiels that are ''not'' asssociated with specific posts.
 +
 
 +
The distinction will be clear in the examples below.
 +
 
 +
== Retrieve a post-related field ==
 +
 
 +
As an example: The home page of a site has ID=7. It has a custom field ''hero text''. How to fetch its value:
  
 
<pre>
 
<pre>
 
<?php
 
<?php
#
 
#
 
###############################################################
 
# Set variables & load libraries
 
###############################################################
 
#
 
echo "Set variables & load libraries...\n";
 
  
$path="/var/www/example.com/";
+
require_once("/var/www/example.com/wp-load.php");
  
require_once($path . "/wp-load.php");
+
the_field("hero_text",7);
 +
</pre>
  
 +
Output:
  
###############################################################
+
<pre>
# ACF - the_field
+
Eat more chips!
###############################################################
+
</pre>
#
 
echo "ACF - the_field...\n";
 
 
 
the_field("hero_text",7);
 
  
?>
+
* Function <code>[[The field (ACF) | the_field()]]</code> displays the value of a field directly
 +
* Alternatively, function <code>[[Get field() (ACF) | get_field()]]</code> returns the value rather than directly displaying it.
  
Uitvoer:
+
== Retrieve an option field ==
  
<pre>
+
Let's fetch the value of a field that is available through ''wp-admin » Options'':
$ php 110.php
 
  
Set variables & load libraries...
+
* Name of the field figured out by looking up its field group at ''wp-admin » Custom fields » Field Groups'': <code>webshop_country_code</code>
 +
* This is a scalar. Not a repeater, array or object. Just a single value
  
 +
== See also ==
  
ACF - get_field...
+
* [[ACF (Advanced Custom Fields, WordPress) | ACF (general)]]
Eet more chips!
+
* [[The field (ACF) | the_field()]]
</pre>
+
* [[Get field() (ACF) | get_field()]]
 +
* [[Update sub field() (ACF) | update_sub_field()]]
 +
* [[Update sub row() (ACF) | update_sub_row()]]
  
== Bronnen ==
+
== Sources ==
  
 
* https://www.advancedcustomfields.com/resources/
 
* https://www.advancedcustomfields.com/resources/
* https://www.advancedcustomfields.com/resources/#functions - Alle PHP-functies bij elkaar!
+
* https://www.advancedcustomfields.com/resources/#functions - All PHP functions together. It's not complete, though!
 +
* https://www.advancedcustomfields.com/resources/code-examples/

Huidige versie van 1 aug 2022 om 11:14

How can you automatically manage ACF fields? In particular, fill in, delete and edit?

Not directly at database level

To start with some bad news: While ACF fields are stored in tables wp_options or wp_postmeta, you cannot write directly to these tables to create ACF fields. Then the 'context' is missing, eg a repeater. So you have to do it via an API call.

Two kinds of fields

There seems to be a distinction between two kinds of ACF fields and how to approach them:

  • Fields that are associated with specific posts
  • Fiels that are not asssociated with specific posts.

The distinction will be clear in the examples below.

Retrieve a post-related field

As an example: The home page of a site has ID=7. It has a custom field hero text. How to fetch its value:

<?php

require_once("/var/www/example.com/wp-load.php");

the_field("hero_text",7);

Output:

Eat more chips!
  • Function the_field() displays the value of a field directly
  • Alternatively, function get_field() returns the value rather than directly displaying it.

Retrieve an option field

Let's fetch the value of a field that is available through wp-admin » Options:

  • Name of the field figured out by looking up its field group at wp-admin » Custom fields » Field Groups: webshop_country_code
  • This is a scalar. Not a repeater, array or object. Just a single value

See also

Sources