Skip to content
multiple-pages-generator

Limited by one project? MPG Pro lets you create unlimited projects, unlimited pages, and gives you priority support.

See Pro Plans →

Using MPG Placeholders in Custom PHP Templates

This guide explains why MPG placeholders like {{mpg_city}} appear as plain text in custom PHP theme templates and how to make them render correctly in those contexts.

How MPG Pages Are Generated

Each MPG project uses a Template page — a regular WordPress page or post that you design in the editor (Gutenberg, Elementor, etc.) and assign in the project settings under the Template dropdown. When a visitor opens a generated URL, MPG intercepts the request, loads the template page's content, replaces all placeholders with data from your source file, and serves the result.

This replacement process happens inside WordPress's the_content filter, which is the standard hook used to process post and page content before it is displayed.

When Custom PHP Templates Are Needed

In most cases, designing your layout inside the MPG Template page in the WordPress editor is sufficient. However, there are situations where a developer builds custom PHP theme templates that output content outside of the standard post content area:

  • A theme has a custom post type template (e.g., single-location.php) that renders additional fields or sections directly in PHP, bypassing the block editor content.
  • A developer builds a header, sidebar, or footer section in a PHP template file that reads values from the post and echoes them directly.
  • A custom theme template uses get_post_meta() to pull values and display them outside the main content loop.

In all these cases, any {{mpg_placeholder}} text output directly in PHP will not be processed because it never passes through the_content filter.

Rendering Placeholders in Custom PHP Templates

To render MPG placeholders inside a custom theme template file, pass the content through apply_filters('the_content', $content) before echoing it. This triggers MPG's replacement engine.

Example — outputting a static string with placeholders:

php
<?php
$content = 'Welcome to {{mpg_city}}! Explore the best services in {{mpg_state}}.';
echo wp_kses_post( apply_filters( 'the_content', $content ) );
?>

Example — reading a post meta value that contains placeholders:

php
<?php
$content = get_post_meta( get_the_ID(), 'your_meta_key', true );
echo wp_kses_post( apply_filters( 'the_content', $content ) );
?>

📝 Note: apply_filters('the_content', $content) runs all filters attached to WordPress post content, not just MPG's. Wrapping the result in wp_kses_post() is a WordPress best practice that allows safe HTML tags while stripping potentially dangerous markup. If you notice unexpected formatting changes, this is WordPress (or other plugins) processing the content as it normally would for post body text.

Common Pitfalls and Troubleshooting

Placeholders still appear as plain text after applying the filter

  • Verify the MPG project is active. Go to MPG > Projects and confirm the project status is active. An inactive project will not process any placeholders.
  • Check placeholder spelling. Placeholder names are derived directly from your data source column headers (CSV, Google Sheet, etc.). For example, a column named city becomes {{mpg_city}}. There is no need to add mpg_ manually — MPG adds that prefix automatically when reading headers.
  • Test in standard post content first. Open the MPG Template page in the WordPress editor, add a block with the same placeholder text, and preview a generated URL. If the placeholder renders there, the issue is specific to your custom template. If it does not render there either, see How to Solve MPG Tags that are Not Parsed?.

apply_filters modifies the content in unexpected ways

Some WordPress plugins (SEO plugins, caching plugins, page builders) attach additional callbacks to the_content filter. If the output is being altered unexpectedly:

  1. Test the template on a staging environment with only MPG and the default theme active.
  2. Progressively re-enable other plugins to identify the conflicting one.
  3. If a specific plugin is interfering, consider using the lower-level MPG replacement function instead:
php
<?php
$project_id = 123; // Replace with your MPG project ID
$content    = 'Welcome to {{mpg_city}}!';

if ( class_exists( 'MPG_CoreModel' ) ) {
    $content = MPG_CoreModel::mpg_shortcode_replacer( $content, $project_id );
}

echo wp_kses_post( $content );
?>

Replace 123 with the numeric ID of your MPG project, which you can find in the URL when editing a project in MPG > Projects.

📝 Note: MPG_CoreModel::mpg_shortcode_replacer() is a lower-level function that only runs MPG's own replacement logic without triggering other the_content filters. This approach is useful when apply_filters('the_content', $content) causes conflicts but should be used only as a fallback, since it bypasses other standard WordPress content processing.

Verifying the Fix

After applying the filter in your template, open a generated URL in your browser and confirm that the placeholder values are replaced with real data from your source file. If you still see {{mpg_city}} instead of an actual city name, review the troubleshooting steps above or consult the MPG Troubleshooting Guide.