Last active
August 6, 2025 17:50
-
-
Save Qubadi/9ed8cfff87d68493c71431a8d2226994 to your computer and use it in GitHub Desktop.
Jetformbuilder rename post-submit action labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UPDATED: 06.08.2025 | |
I've developed a custom code for JetFormBuilder post-submit action, which allows you to easily edit the label name, | |
something that's not possible by default in JetFormBuilder. | |
Copy the following PHP and create a PHP snippet using your snippet plugins. | |
Paste the code into the plugin and save it. | |
___________________________- | |
<?php | |
/** | |
* Description: Adds a small Rename button to JetFormBuilder actions. Stores custom labels in _aurora_jfb_labels_v1 (by action id) and overlays them in the editor. Does not modify _jf_actions. | |
* Version: 1.0.3 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
if ( ! class_exists( 'JFB_Dup_Rename_Actions' ) ) : | |
final class JFB_Dup_Rename_Actions { | |
private static $inst; | |
private $script_loaded = false; | |
public static function instance() { | |
return self::$inst ?? self::$inst = new self(); | |
} | |
private function __construct() { | |
if ( is_admin() ) { | |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] ); | |
add_action( 'admin_footer', [ $this, 'assets' ] ); | |
add_action( 'wp_ajax_aurora_get_labels', [ $this, 'ajax_get_labels' ] ); // renamed | |
add_action( 'wp_ajax_aurora_set_label', [ $this, 'ajax_rename' ] ); // renamed | |
} | |
} | |
private function current_post_id_from_request(): int { | |
$pid = 0; | |
if ( isset( $_GET['post'] ) ) { | |
$pid = intval( $_GET['post'] ); | |
} elseif ( isset( $_POST['post_ID'] ) ) { | |
$pid = intval( $_POST['post_ID'] ); | |
} else { | |
global $post; | |
if ( is_object( $post ) && isset( $post->ID ) ) { | |
$pid = intval( $post->ID ); | |
} | |
} | |
return $pid; | |
} | |
public function enqueue( $hook ) { | |
// Load on JFB form edit screens (classic & GB) | |
$load = false; | |
if ( in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) { | |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; | |
if ( $screen && $screen->post_type === 'jet-form-builder' ) { | |
$load = true; | |
} | |
} | |
if ( strpos( $hook, 'jet-form-builder' ) !== false ) { | |
$load = true; | |
} | |
if ( ! $load ) return; | |
if ( ! $this->script_loaded ) { | |
$post_id = $this->current_post_id_from_request(); | |
wp_enqueue_script( 'jquery' ); | |
wp_register_script( 'jfb-dummy', '' ); | |
wp_enqueue_script( 'jfb-dummy' ); | |
wp_localize_script( | |
'jfb-dummy', | |
'jfbDupRen', | |
[ | |
'ajax' => admin_url( 'admin-ajax.php' ), | |
'nonce' => wp_create_nonce( 'jfb_dup_rename_nonce' ), | |
'postId' => $post_id, // *** provide real post ID from PHP *** | |
] | |
); | |
$this->script_loaded = true; | |
} | |
} | |
/** Return all saved labels for this post */ | |
public function ajax_get_labels() { | |
check_ajax_referer( 'jfb_dup_rename_nonce', 'nonce' ); | |
$post_id = intval( $_POST['post_id'] ?? 0 ); | |
if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { | |
wp_send_json_error( 'Bad request.' ); | |
} | |
$labels = get_post_meta( $post_id, '_aurora_jfb_labels_v1', true ); // renamed key | |
if ( ! is_array( $labels ) ) $labels = []; | |
$out = []; | |
foreach ( $labels as $id => $lab ) { | |
$out[ (string) intval( $id ) ] = sanitize_text_field( $lab ); | |
} | |
wp_send_json_success( [ 'labels' => $out ] ); | |
} | |
/** Save ONE label into _aurora_jfb_labels_v1[action_id] */ | |
public function ajax_rename() { | |
check_ajax_referer( 'jfb_dup_rename_nonce', 'nonce' ); | |
$action_id = intval( $_POST['action_id'] ?? 0 ); | |
$post_id = intval( $_POST['post_id'] ?? 0 ); | |
$label = sanitize_text_field( wp_unslash( $_POST['label'] ?? '' ) ); | |
if ( ! $action_id || ! $post_id || $label === '' ) { | |
wp_send_json_error( 'Bad request.' ); | |
} | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
wp_send_json_error( 'Unauthorized.' ); | |
} | |
$labels = get_post_meta( $post_id, '_aurora_jfb_labels_v1', true ); // renamed key | |
if ( ! is_array( $labels ) ) $labels = []; | |
$labels[ (string) $action_id ] = $label; | |
$ok = update_post_meta( $post_id, '_aurora_jfb_labels_v1', $labels ); // renamed key | |
if ( ! $ok ) { | |
wp_send_json_error( 'Save failed.' ); | |
} | |
wp_send_json_success( [ 'label' => $label, 'labels' => $labels ] ); | |
} | |
public function assets() { ?> | |
<style id="jfb-css"> | |
.jfb-dup-controls{display:flex!important;align-items:center!important;gap:6px!important;flex-wrap:nowrap!important} | |
.jfb-dup-controls, .jet-form-builder-action__controls, .jet-form-action-controls {overflow: visible !important;} | |
.jfb-btn{background:transparent;border:0;padding:0;line-height:0;cursor:pointer;display:inline-flex;align-items:center;opacity:0;visibility:hidden;transition:opacity .15s ease;position:relative} | |
.jfb-dup-meta:hover .jfb-btn{opacity:.9;visibility:visible} | |
.jfb-btn svg{width:15px;height:15px;stroke:currentColor;fill:none;stroke-width:1.8} | |
.jfb-btn.loading{pointer-events:none;opacity:.35} | |
.jfb-btn .jfb-tooltip{visibility:hidden;opacity:0;position:absolute;bottom:100%;left:50%;transform:translateX(-50%);margin-bottom:4px;background:rgba(0,0,0,.85);color:#fff;font-size:12px;line-height:1;padding:6px 8px;border-radius:2px;white-space:nowrap;z-index:10000;transition:opacity .15s ease,visibility .15s ease;pointer-events:none} | |
.jfb-btn:hover .jfb-tooltip{visibility:visible;opacity:1} | |
</style> | |
<script id="jfb-js"> | |
(function($, wp){ | |
'use strict'; | |
if (window.jfbDupRenInit) return; | |
window.jfbDupRenInit = true; | |
const ROWS = ['.jet-form-builder-action','.components-panel__body .jet-form-action','.jet-fb-action-item','[data-action-type]'].join(','); | |
const CTRL = ['.jet-form-builder-action__controls','.jet-form-action__controls','.jet-form-action-controls','.components-flex.f13vj9vm','[class*="__controls"]'].join(','); | |
const TITLE = '.jet-form-builder-action__title, .jet-form-action__title, .a14pz2hj'; | |
const isGB = !!(wp && wp.data && wp.data.select); | |
const POST_ID = parseInt(jfbDupRen.postId, 10) || (function(){ | |
// Fallbacks only if PHP couldn't resolve it | |
if (isGB) { | |
try { return parseInt(wp.data.select('core/editor').getCurrentPostId() || 0, 10); } catch(e){} | |
} | |
const v = $('#post_ID').val(); | |
return v ? parseInt(v, 10) : 0; | |
})(); | |
let LABELS = {}; | |
let ticking = false; | |
function tick(fn){ if (ticking) return; ticking = true; setTimeout(()=>{ ticking=false; fn(); }, 60); } | |
function getActionsArr() { | |
// Read JetFB meta to map row index -> action -> id | |
if (isGB) { | |
try { | |
const meta = wp.data.select('core/editor').getEditedPostAttribute('meta') || {}; | |
try { return JSON.parse(meta._jf_actions || '[]'); } catch(e) { return []; } | |
} catch(e){ return []; } | |
} | |
const el = $('input[name="_jf_actions"]'); | |
if (!el.length) return []; | |
try { return JSON.parse(el.val() || '[]'); } catch(e){ return []; } | |
} | |
function actionIdByRow($row){ | |
const idx = $(ROWS).index($row); | |
if (idx < 0) return null; | |
const arr = getActionsArr(); | |
const a = arr[idx]; | |
return a && a.id ? parseInt(a.id, 10) : null; | |
} | |
function applyLabel($row, id){ | |
if (!id) return; | |
const lab = LABELS[String(id)]; | |
if (!lab) return; | |
const $t = $row.find(TITLE).first(); | |
if ($t.length) $t.text(lab); | |
$row.attr('data-jfb-action-id', String(id)); | |
} | |
function applyAll(){ | |
$(ROWS).each(function(){ | |
const $r = $(this); | |
const id = actionIdByRow($r); | |
if (id) applyLabel($r, id); | |
}); | |
} | |
function fetchLabels(){ | |
if (!POST_ID) return; | |
$.post(jfbDupRen.ajax, { | |
action: 'aurora_get_labels', // renamed | |
nonce: jfbDupRen.nonce, | |
post_id: POST_ID | |
}, function(resp){ | |
if (resp && resp.success && resp.data && resp.data.labels) { | |
LABELS = resp.data.labels || {}; | |
applyAll(); | |
} | |
}, 'json'); | |
} | |
function promptLabel(current, cb){ | |
if (wp?.components?.Modal && wp?.element) { | |
const { createElement, useState } = wp.element; | |
const { Modal, TextControl, Button } = wp.components; | |
const mount = document.createElement('div'); | |
document.body.appendChild(mount); | |
const UI = () => { | |
const [val, setVal] = useState(current); | |
const close = () => { wp.element.unmountComponentAtNode(mount); mount.remove(); }; | |
return createElement(Modal, { title: 'Rename Action', onRequestClose: close }, | |
createElement(TextControl, { label:'Action label', value:val, onChange:setVal, autoFocus:true }), | |
createElement('div', { style:{marginTop:16,display:'flex',gap:8} }, | |
createElement(Button, { isPrimary:true, onClick:()=>{ const v=(val||'').trim()||current; close(); cb(v);} }, 'Save'), | |
createElement(Button, { onClick:close }, 'Cancel') | |
) | |
); | |
}; | |
wp.element.render(createElement(UI), mount); | |
} else { | |
const v = window.prompt('Label:', current); | |
if (v !== null) cb((v||'').trim() || current); | |
} | |
} | |
const svgEdt = '<svg viewBox="0 0 24 24"><path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 1 1 3 3L7 19l-4 1 1-4 12.5-12.5Z"/></svg>'; | |
function ensureCtrl($row){ | |
const $c = $row.find(CTRL).first(); | |
if (!$c.length) return null; | |
return $c.addClass('jfb-dup-controls'); | |
} | |
function renameRow($row, btn){ | |
if (!POST_ID) { alert('Please save the form first.'); return; } | |
const id = actionIdByRow($row); | |
if (!id) return; | |
const $t = $row.find(TITLE).first(); | |
const cur = ($t.text() || '').trim(); | |
promptLabel(cur, function(lab){ | |
$(btn).addClass('loading'); | |
$.post(jfbDupRen.ajax, { | |
action: 'aurora_set_label', // renamed | |
nonce: jfbDupRen.nonce, | |
action_id: id, | |
post_id: POST_ID, | |
label: lab | |
}, function(resp){ | |
$(btn).removeClass('loading'); | |
if (resp && resp.success) { | |
LABELS[String(id)] = lab; | |
if ($t.length) $t.text(lab); | |
} else { | |
alert('Save failed.'); | |
} | |
}, 'json').fail(function(){ | |
$(btn).removeClass('loading'); | |
alert('Network error.'); | |
}); | |
}); | |
} | |
function inject(row){ | |
const $r = $(row); | |
if ($r.hasClass('jfb-dup-meta')) return; | |
const $ctrl = ensureCtrl($r); if (!$ctrl) return; | |
if (!$ctrl.find('.jfb-edit').length) { | |
const $btn = $('<button>', { class:'jfb-btn jfb-edit', html:svgEdt, type:'button' }); | |
$btn.append($('<span>').addClass('jfb-tooltip').text('Rename')); | |
$btn.on('click', function(e){ e.stopPropagation(); renameRow($r, this); }); | |
$ctrl.append($btn); | |
} | |
$r.addClass('jfb-dup-meta'); | |
const id = actionIdByRow($r); | |
if (id) applyLabel($r, id); | |
} | |
function scan(){ $(ROWS).each((_, el) => inject(el)); applyAll(); } | |
new MutationObserver(()=>tick(scan)).observe(document.body, { childList:true, subtree:true }); | |
scan(); | |
fetchLabels(); | |
})(jQuery, window.wp); | |
</script> | |
<?php } | |
} | |
add_action( 'plugins_loaded', [ 'JFB_Dup_Rename_Actions', 'instance' ] ); | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment