# Americor JSON LD Setup
## Add Json LD
This code has two main functions: adding a global script to the head of your website's pages and outputting structured data (also known as JSON-LD schema) for individual posts.
Here's an explanation of each part of the code:
1. **add_global_script()**: This function adds a JavaScript script to the head of your website. This script initializes Smartlook, a visitor recording tool, and inserts structured data about the website. The Smartlook script will enable you to record and review visitor interactions on your site. The structured data script provides information about your website to search engines in a standardized format, which can enhance SEO and usability.
2. **output_json_ld_article_schema($post_id)**: This function generates structured data for individual posts. The structured data describes various elements of the post, including its headline, publishing date, author, and more. Like the website-level structured data, this post-level structured data can enhance SEO and usability.
3. **add_json_ld_schema_to_single_posts()**: This function calls the `output_json_ld_article_schema` function, but only if the current page is a single post. It's hooked to the `wp_footer` action, so the structured data is output in the footer of the website.
To insert this code into your theme's `functions.php` file, follow these steps:
1. Open the `functions.php` file of your active theme. You can do this via FTP or the file manager in your hosting control panel. Alternatively, you can navigate to "Appearance > Theme Editor" in the WordPress admin area, select the active theme, and then open the `functions.php` file.
2. Copy the provided code and paste it at the end of the `functions.php` file. Be careful not to overwrite any existing code.
3. Save the changes to the `functions.php` file.
Please note that modifications to the `functions.php` file are theme-specific. If you switch themes, you'll need to reapply these changes to the new theme's `functions.php` file. To avoid this, you could create a site-specific plugin and add the code there.
```php
add_action('wp_head', 'add_global_script');
function output_json_ld_article_schema($post_id) {
$post_data = get_post($post_id);
$post_author_id = $post_data->post_author;
$reviewed_by_field = get_field('reviewed_by', $post_id);
if ($post_author_id && $reviewed_by_field) {
$author_data = array(
"@type" => "Person",
"name" => get_the_author_meta('display_name', $post_author_id),
"givenName" => get_the_author_meta('first_name', $post_author_id),
"familyName" => get_the_author_meta('last_name', $post_author_id),
"url" => get_author_posts_url($post_author_id),
"email" => get_the_author_meta('user_email', $post_author_id),
"image" => get_avatar_url($post_author_id),
"jobTitle" => "Author",
"description" => get_the_author_meta('description', $post_author_id),
"worksFor" => array(
"@type" => "Organization",
"name" => "Americor"
),
"reviewedBy" => array(
"@type" => "Person",
"name" => $reviewed_by_field["display_name"],
"url" => get_author_posts_url($reviewed_by_field["ID"]),
)
);
$json_ld = array(
"@context" => "https://schema.org",
"@type" => "Article",
"mainEntityOfPage" => array(
"@type" => "WebPage",
"@id" => get_the_permalink($post_id),
),
"headline" => get_the_title($post_id),
"datePublished" => get_the_date(DATE_ATOM, $post_id),
"dateModified" => get_the_modified_date(DATE_ATOM, $post_id),
"author" => $author_data,
"publisher" => array(
"@type" => "Organization",
"name" => "Americor",
"logo" => array(
"@type" => "ImageObject",
"url" => "https://americor.com/wp-content/uploads/2023/03/unnamed-3.gif"
),
),
"description" => $post_data->post_excerpt,
"image" => array(
"@type" => "ImageObject",
"url" => get_the_post_thumbnail_url($post_id),
"width" => 611, // you may need to adjust this
"height" => 575, // you may need to adjust this
),
);
echo '<script type="application/ld+json">' . PHP_EOL;
echo json_encode($json_ld, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
echo PHP_EOL . '</script>' . PHP_EOL;
}
}
function add_json_ld_schema_to_single_posts() {
if (is_single()) {
output_json_ld_article_schema(get_the_ID());
}
}
add_action('wp_footer', 'add_json_ld_schema_to_single_posts');
add_filter( 'wpseo_json_ld_output', '__return_false' );
```
## Bulk Update Reviwed
### Instructions
The code you provided is a WordPress plugin written in PHP. It's designed to perform a bulk update of an Advanced Custom Fields (ACF) field called "reviewed_by". Here's how to run it:
1. **Save the plugin code**: Create a new directory inside the WordPress `wp-content/plugins` directory and name it `acf-bulk-update-reviewed-by`. Inside this directory, create a file called `acf-bulk-update-reviewed-by.php` and paste the given code into this file.
2. **Install and activate the plugin**: Go to your WordPress admin dashboard. From the sidebar, navigate to "Plugins > Installed Plugins". You should see the "ACF Bulk Update Reviewed By" plugin on the list. Click “Activate” to enable the plugin.
3. **Run the plugin**: Now you can trigger the function by adding `?acf_bulk_update=1` to the end of your site's URL. For example, if your site's URL is `http://example.com`, you can trigger the function by navigating to `http://example.com?acf_bulk_update=1`. This action will run the code to bulk update the “reviewed_by” ACF field.
Please note:
- This code is designed to work in a WordPress environment with the Advanced Custom Fields (ACF) plugin installed and active.
- The code sets a specific reviewer (user with the ID 14) and queries posts authored by a specific author (user with the ID 15). You might need to adjust these IDs according to your WordPress installation.
- Be cautious when running bulk operations like this, as it could potentially change a large number of posts. Always back up your database before running operations like these.
### Plugin code
```php
<?php
/**
* Plugin Name: ACF Bulk Update Reviewed By
* Description: A simple plugin to bulk update the “reviewed_by" ACF field.
* Version: 1.1
* Author: Lucas Bezerra
*/
function acf_bulk_update_reviewed_by() {
if (isset($_GET['acf_bulk_update']) && $_GET['acf_bulk_update'] == '1') {
// Set the reviewer ID
$reviewer_id = 14;
$reviewer_data = array(
'ID' => $reviewer_id,
'user_firstname' => get_the_author_meta('first_name', $reviewer_id),
'user_lastname' => get_the_author_meta('last_name', $reviewer_id),
'nickname' => get_the_author_meta('nickname', $reviewer_id),
'user_nicename' => get_the_author_meta('user_nicename', $reviewer_id),
'display_name' => get_the_author_meta('display_name', $reviewer_id),
'user_email' => get_the_author_meta('user_email', $reviewer_id),
'user_url' => get_the_author_meta('user_url', $reviewer_id),
'user_registered' => get_the_author_meta('user_registered', $reviewer_id),
'user_description' => get_the_author_meta('description', $reviewer_id),
'user_avatar' => get_avatar_url($reviewer_id),
);
// Query all posts where the post author is equal to 14
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'author' => 15,
);
$posts = get_posts($args);
// Initialize a counter for the number of updated posts
$updated_posts = 0;
// Loop through the posts and update the "reviewed_by” ACF field
foreach ($posts as $post) {
if (update_field('reviewed_by', $reviewer_data, $post->ID)) {
$updated_posts++;
}
}
// Provide visual feedback
echo '<h2>ACF Bulk Update Results</h2>';
echo '<p>Total posts processed: ' . count($posts) . '</p>';
echo '<p>Total posts updated: ' . $updated_posts . '</p>';
exit;
}
}
add_action('init', 'acf_bulk_update_reviewed_by');
```