Get CF7 shortcode by ID
August 7, 2025 · AI Conversations
**Example**
If your form ID is `123`, you can directly write:
echo do_shortcode('Error: Contact form not found.
');
If you also want to include the form’s title:
echo do_shortcode('Error: Contact form not found.
');
—
**If you want to dynamically get it from the database by ID**
$form_id = 123; // Your form ID
$title = get_the_title($form_id);
echo do_shortcode('Error: Contact form not found.
');
This way, you don’t have to hardcode the title.
—
Do you want me to give you a **function that returns the Contact Form 7 shortcode just by passing the form ID** so you can use it anywhere? That will make it reusable.
function csos_init_session()
{
// Check if we're on a relevant plugin page
$current_page = get_query_var('pagename');
$plugin_pages = array(
'keuzehulp-duurzame-technologie',
'resultaat',
);
if ( in_array($current_page, $plugin_pages) ) {
session_start();
}
// Optional: also allow via query vars
if ( get_query_var('questioncode') || get_query_var('resultaatcode') ) {
if ( !session_id() ) {
session_start();
}
}
}START session like this in wordpress custom plugin not sarting seesion
1. **`get_query_var()` won’t return anything unless you’ve registered the query vars first**
2. **`pagename` query var doesn’t always exist on `init`** (depending on permalink structure)
3. **Sessions must be started very early** — before any output (and preferably before most WordPress hooks that print content).
—
### ✅ Correct Way to Start a Session in a Custom Plugin
add_action('init', 'csos_init_session', 1); // priority 1 so it runs before most things
function csos_init_session() {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
}
—
### If You Only Want to Start for Specific Pages
add_action('template_redirect', 'csos_start_session_on_pages');
function csos_start_session_on_pages() {
$plugin_pages = array(
'keuzehulp-duurzame-technologie',
'resultaat',
);
if (is_page($plugin_pages) || get_query_var('questioncode') || get_query_var('resultaatcode')) {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
}
}
**Why `template_redirect` instead of `init`?**
– By the time `template_redirect` runs, WordPress has already resolved the queried object (so `is_page()` works properly).
– Still runs before output starts, so sessions can be created safely.
—
### If You Want `get_query_var()` to Work
Before using custom query vars like `questioncode` or `resultaatcode`, you must register them:
add_filter('query_vars', function($vars) {
$vars[] = 'questioncode';
$vars[] = 'resultaatcode';
return $vars;
});
—
💡 **Tip:** If your plugin needs sessions across multiple requests (like storing answers for a quiz), I recommend always starting the session early (priority `1` on `init`) instead of checking the page every time — it’s more reliable.
—
If you want, I can rewrite your current function so it:
– Works in a plugin without theme dependencies
– Only starts sessions for your 2 pages or if custom query vars exist
– Avoids "headers already sent" errors.
Do you want me to rewrite it that way?