Skip to content

Export as PHP

Export field groups as clean PHP code to use in your theme or plugin without OneMeta installed.

OneMeta can export any field group as a clean, standalone PHP file. Once exported, you can use the field group in any WordPress theme or plugin — OneMeta does not need to be installed on the site.

This is particularly useful for ThemeForest themes, client projects, and distributable plugins where you cannot require end users to install a specific plugin for your fields to work.


How to Export

From the Builder:

  1. Open any field group in the builder
  2. Click the Export PHP button in the actions bar
  3. A modal will appear showing the generated PHP code
  4. Click Copy to Clipboard or Download PHP File

From the Dashboard:

  1. Go to OneMeta → Field Groups
  2. Click the Export icon on any field group card
  3. Copy or download the generated code

From the Documentation page:

  1. Go to OneMeta → Documentation
  2. Scroll to Export Field Groups
  3. Find your field group and click Export PHP

What the Export Looks Like

The exported file uses the onemeta_field_groups filter hook to register the field group:

php

<?php
/**
 * OneMeta Field Group: Hero Fields
 * Generated on: 2025-12-25 10:00:00
 *
 * @package OneMeta
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_filter( 'onemeta_field_groups', function( $groups ) {

    $groups['hero_fields'] = [
        'title'     => 'Hero Fields',
        'type'      => 'post',
        'post_type' => 'post',
        'position'  => 'normal',
        'fields'    => [
            'hero_title' => [
                'type'        => 'text',
                'label'       => 'Hero Title',
                'description' => 'Main heading for the hero section',
                'placeholder' => 'Enter a title...',
                'default'     => '',
            ],
            'hero_image' => [
                'type'  => 'image',
                'label' => 'Hero Image',
            ],
        ],
    ];

    return $groups;
} );

Where to Put the Exported Code

Option A — Theme functions.php

Paste the exported code directly into your theme’s functions.php. Best for simple setups.

Option B — Separate file (recommended)

Create a dedicated file in your theme, e.g. inc/meta-fields.php, and paste the code there. Then include it from functions.php:

php

// functions.php
require_once get_template_directory() . '/inc/meta-fields.php';

This keeps your functions.php clean and your field groups organised.

Option C — Plugin

If you are distributing a plugin, include the exported file in your plugin and require it from your main plugin file.


Using the Fields After Export

Once the exported PHP is loaded, you retrieve field values exactly the same way — using onemeta_get_meta() and onemeta_get_user_meta(). The helper functions are provided by OneMeta if it is installed, but if it is not installed you need to include your own fallback:

php

// Simple fallback helper (add to functions.php if OneMeta may not be installed)
if ( ! function_exists( 'onemeta_get_meta' ) ) {
    function onemeta_get_meta( int $post_id, string $key, mixed $default = '' ): mixed {
        $meta_key = 'onemeta_' . $key;
        if ( ! metadata_exists( 'post', $post_id, $meta_key ) ) {
            return $default;
        }
        $value = get_post_meta( $post_id, $meta_key, true );
        return ( $value !== '' && $value !== null ) ? $value : $default;
    }
}

Notes

  • Exported PHP registers fields via a filter hook — fields are not saved to the database when using this method
  • The meta box still appears in the WordPress post editor when the exported PHP is loaded
  • Field values are stored in wp_postmeta using the onemeta_ prefix — the same as when using the plugin directly
  • You can mix both approaches — use the plugin for some field groups and exported PHP for others

Was this article helpful?

On This Page