Created
June 22, 2025 16:38
-
-
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
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. | |
___________________________- | |
/** | |
* JetFormBuilder – Rename Post-Submit Actions Only | |
*/ | |
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_jfb_rename_action', [ $this, 'ajax_rename' ] ); | |
} | |
} | |
public function enqueue( $hook ) { | |
if ( str_contains( $hook, 'jet-form-builder' ) ) { | |
if ( ! $this->script_loaded ) { | |
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' ), | |
] | |
); | |
$this->script_loaded = true; | |
} | |
} | |
} | |
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( $_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.' ); | |
} | |
$raw = get_post_meta( $post_id, '_jf_actions', true ); | |
$actions = is_string( $raw ) ? json_decode( $raw, true ) : []; | |
if ( ! is_array( $actions ) ) { | |
wp_send_json_error( 'Invalid meta.' ); | |
} | |
$found = false; | |
foreach ( $actions as &$action ) { | |
if ( isset( $action['id'] ) && intval( $action['id'] ) === $action_id ) { | |
$this->deep_set_label( $action, $label ); | |
$found = true; | |
break; | |
} | |
} | |
if ( ! $found ) { | |
wp_send_json_error( 'Action not found.' ); | |
} | |
update_post_meta( $post_id, '_jf_actions', wp_json_encode( $actions ) ); | |
wp_send_json_success(); | |
} | |
private function deep_set_label( array &$node, $label ) : void { | |
foreach ( $node as $k => &$v ) { | |
if ( in_array( $k, [ 'name', 'label', 'title', 'action_name' ], true ) ) { | |
$v = $label; | |
} elseif ( is_array( $v ) ) { | |
$this->deep_set_label( $v, $label ); | |
} | |
} | |
} | |
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:.85;visibility:visible}.jfb-btn:hover{opacity:1} | |
.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.loading:after{content:'';margin-left:4px;width:10px;height:10px;border:1px solid transparent;border-top-color:currentColor;border-radius:50%;animation:spin 1s linear infinite} | |
@keyframes spin{to{transform:rotate(360deg)}} | |
.jfb-btn .jfb-tooltip { | |
visibility: hidden; | |
opacity: 0; | |
position: absolute; | |
bottom: 100%; | |
left: 50%; | |
transform: translateX(calc(-50% - 20px)); | |
margin-bottom: 4px; | |
background: rgba(0, 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 isGB = !!(wp?.data?.select); | |
function notice(msg) { | |
if (isGB && wp?.data?.dispatch) { | |
const id = wp.data.dispatch('core:notices').createNotice('success', msg, { isDismissible: true }); | |
setTimeout(() => wp.data.dispatch('core:notices').removeNotice(id, 'global'), 2000); | |
} | |
} | |
function loadLabels() { | |
try { return JSON.parse(localStorage.getItem('jfbActionLabels') || '{}'); } | |
catch (e) { return {}; } | |
} | |
function saveLabels(obj) { | |
try { localStorage.setItem('jfbActionLabels', JSON.stringify(obj)); } | |
catch (e) {} | |
} | |
function setAllKeys(o, l) { | |
o.name = o.label = o.title = o.action_name = l; | |
o.settings = o.settings || {}; | |
o.settings.label = o.settings.title = o.settings.action_name = l; | |
} | |
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, set ] = 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: set, autoFocus: true }), | |
createElement('div', { style: { marginTop: 16, display: 'flex', gap: 8 } }, | |
createElement(Button, { isPrimary: true, onClick: () => { close(); cb(val.trim() || current); } }, 'Save'), | |
createElement(Button, { onClick: close }, 'Cancel') | |
) | |
); | |
}; | |
wp.element.render(createElement(UI), mount); | |
} else { | |
const val = window.prompt('Label:', current); | |
if (val !== null) cb(val.trim() || current); | |
} | |
} | |
function getActionsArr() { | |
if (isGB && wp?.data?.select) { | |
const meta = wp.data.select('core/editor').getEditedPostAttribute('meta') || {}; | |
try { return JSON.parse(meta._jf_actions || '[]'); } catch (e) { return []; } | |
} else { | |
const el = $('input[name="_jf_actions"]'); | |
if (!el.length) { return []; } | |
try { return JSON.parse(el.val() || '[]'); } catch (e) { return []; } | |
} | |
} | |
function renameRow($row) { | |
const idx = $(ROWS).index($row); | |
const domEl = $row.find('.jet-form-builder-action__title, .jet-form-action__title, .a14pz2hj').first(); | |
let cur = domEl.text().trim(); | |
if (idx === -1) return; | |
const actions = getActionsArr(); | |
const action = actions[idx]; | |
if (!action?.id) return; | |
const id = action.id; | |
promptLabel(cur, (lab) => { | |
const stored = loadLabels(); | |
stored[id] = lab; | |
saveLabels(stored); | |
domEl.text(lab); | |
notice('Label updated.'); | |
if (isGB) { | |
const arr = actions; | |
const i = arr.findIndex(a => a.id === id); | |
if (i > -1) { | |
setAllKeys(arr[i], lab); | |
wp.data.dispatch('core/editor').editPost({ meta: { _jf_actions: JSON.stringify(arr) } }); | |
} | |
} else { | |
const postId = parseInt($('#post_ID').val(), 10) || 0; | |
if (postId) { | |
$.post(jfbDupRen.ajax, { | |
action: 'jfb_rename_action', | |
nonce: jfbDupRen.nonce, | |
action_id: id, | |
post_id: postId, | |
label: lab, | |
}, () => location.reload(), 'json'); | |
} | |
} | |
}); | |
} | |
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 applyCustomLabel($row, id) { | |
const stored = loadLabels(); | |
if ( stored[id] ) { | |
$row.find('.jet-form-builder-action__title, .jet-form-action__title, .a14pz2hj').first().text(stored[id]); | |
} | |
} | |
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 }); | |
$btn.append($('<span>').addClass('jfb-tooltip').text('Rename')); | |
$btn.on('click', e => { e.stopPropagation(); renameRow($r); }); | |
$ctrl.append($btn); | |
} | |
$r.addClass('jfb-dup-meta'); | |
const idx = $(ROWS).index($r); | |
const actions = getActionsArr(); | |
const action = actions[idx]; | |
if ( action?.id ) { | |
$r.attr('data-jfb-action-id', action.id); | |
applyCustomLabel($r, action.id); | |
} | |
} | |
function scan() { $(ROWS).each((_, el) => inject(el)); } | |
new MutationObserver(scan).observe(document.body, { childList: true, subtree: true }); | |
scan(); | |
})(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