Debug Output:
Post ID: 932
Post Type: wpcode
Post Status: draft
Post Title: Post submit shortcode B
Post Content:
Post ID: 932
Post Type: wpcode
Post Status: draft
Post Title: Post submit shortcode B
Post Content:
function custom_post_form_shortcode() {
if (!is_user_logged_in()) return '<div style="padding:15px; margin-top:50px;" class="notice-div-wrapper"><div style="text-align:center;background:#333; padding:15px 5px; border-radius:10px;" class="notice-div">You must be logged in to create a post.<p style="text-align:center; margin-top:-10px;"><button style="" onclick="userlogin()" class="notice-btn-left">Login »</button><span style="padding-left:12px;"><button style="background:grey;color:black !important;" onclick="addpost()" class="notice-btn-right">« Back</button></span></p></div></div>';
$user = wp_get_current_user();
$is_pro = in_array('pro', $user->roles);
// ✅ Post limit check here is 100% fine
if (!$is_pro) {
$args = [
'author' => $user->ID,
'post_type' => 'post',
'post_status' => 'publish',
'date_query' => [['after' => '24 hours ago']],
'posts_per_page' => 1
];
$recent_query = new WP_Query($args);
if ($recent_query->have_posts()) {
return '<div style="padding:25px; margin-top:50px;" class="notice-div-wrapper"><div style="text-align:center;background:#333; padding:15px 5px; border-radius:10px;" class="notice-div">You have reached the once daily limit for posting. To post more than once daily kindly verify your account using the button below<p style="text-align:center; margin-top:0px;"><button style="" onclick="userverify()" class="notice-btn-left">Verify Now »</button><span style="padding-left:12px;"><button style="background:grey;color:black !important;" onclick="addpost()" class="notice-btn-right">« Back</button></span></p></div></div>';
}
}
$nonce = wp_create_nonce('custom_post_form_nonce');
ob_start();
?>
<form id="custom-post-form">
<div>
<textarea style="color: #000;" name="post_excerpt" required rows="3" placeholder="What's on your mind?..."></textarea>
</div>
<div id="content-area">
<div id="media-preview" class="media-scroll">
<div id="upload-box" class="media-box upload-box">+</div>
</div>
<input id="post_content" name="post_content" type="hidden" />
<input id="media-upload" style="display: none;" accept="image/*,video/*" type="file" />
<input id="auto-category" name="post_category" type="hidden" />
<input id="featured_data" name="featured_data" type="hidden" />
<div class="meta-field" style="display:none;">
<select name="meta_field_value" id="meta_field_value">
<option value="">Select type (optional)</option>
<option value="funny30">Funny30</option>
<option value="movie">Movie</option>
<option value="skit">Skit</option>
<option value="edu">Education</option>
<option value="others">Others</option>
</select>
<input type="hidden" name="meta_field_key" value="type">
</div>
</div>
<div class="form-actions">
<div id="media-toggle" onclick="mediatoggle ()"><i class="fas fa-image"></i></div>
<button type="submit" id="publish-button">Publish</button>
<button type="button" id="cancel-button" onclick="pbtn()">Cancel</button>
</div>
</form>
<div id="custom-confirm-overlay">
<div id="custom-confirm-box">
<p class="confirm-message">Remove this media?</p>
<div class="buttons">
<button id="confirm-no">Cancel</button>
<button id="confirm-yes">Remove</button>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.15.0/Sortable.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const ajaxUrl = "<?= admin_url('admin-ajax.php') ?>";
const nonce = "<?= esc_js($nonce) ?>";
const mediaInput = document.getElementById('media-upload');
const uploadBox = document.getElementById('upload-box');
const publishButton = document.getElementById('publish-button');
const previewContainer = document.getElementById('media-preview');
const featuredInput = document.getElementById('featured_data');
const metaField = document.querySelector('.meta-field');
let mediaFiles = [], mediaElements = [], mediaSizes = [], mediaStatusLabels = [];
let videoPosterBlob = null;
let firstImageUrl = null;
const confirmOverlay = document.getElementById('custom-confirm-overlay');
let confirmCallback = null;
function humanSize(bytes) {
const mb = bytes / (1024 * 1024);
return mb > 1 ? mb.toFixed(1) + 'MB' : (bytes / 1024).toFixed(0) + 'KB';
}
function showConfirmDialog(msg, callback) {
confirmOverlay.querySelector('.confirm-message').textContent = msg;
confirmCallback = callback;
confirmOverlay.style.display = 'flex';
}
document.getElementById('confirm-no').onclick = () => {
confirmOverlay.style.display = 'none';
confirmCallback = null;
};
document.getElementById('confirm-yes').onclick = () => {
if (typeof confirmCallback === 'function') confirmCallback();
confirmOverlay.style.display = 'none';
};
uploadBox.addEventListener('click', () => {
if (!uploadBox.classList.contains('disabled')) mediaInput.click();
});
mediaInput.addEventListener('change', () => {
const file = mediaInput.files[0];
if (!file) return;
const isVideo = file.type.startsWith('video');
const isImage = file.type.startsWith('image');
const videoCount = mediaFiles.filter(f => f.type.startsWith('video')).length;
const imageCount = mediaFiles.filter(f => f.type.startsWith('image')).length;
if ((isVideo && (videoCount > 0 || imageCount > 0)) ||
(isImage && (videoCount > 0 || imageCount >= 5))) return;
previewMedia(file);
updateUploadBoxState();
});
function updateUploadBoxState() {
const videoCount = mediaFiles.filter(f => f.type.startsWith('video')).length;
const imageCount = mediaFiles.filter(f => f.type.startsWith('image')).length;
uploadBox.classList.toggle('disabled', videoCount > 0 || imageCount >= 5);
}
function previewMedia(file) {
const reader = new FileReader();
reader.onload = () => {
const blobUrl = reader.result;
const wrapper = document.createElement('div');
wrapper.className = 'media-box';
wrapper.dataset.filename = file.name;
wrapper.setAttribute('draggable', 'true');
const removeBtn = document.createElement('div');
removeBtn.className = 'remove-media';
removeBtn.innerHTML = '<i class="fa fa-trash"></i>';
removeBtn.title = 'Remove';
removeBtn.onclick = (e) => {
e.stopPropagation();
showConfirmDialog("Remove this media?", () => {
const index = mediaFiles.findIndex(f => f.name === file.name);
if (index !== -1) {
mediaFiles.splice(index, 1);
mediaElements.splice(index, 1);
mediaSizes.splice(index, 1);
mediaStatusLabels.splice(index, 1);
}
wrapper.remove();
updateUploadBoxState();
toggleMetaField();
});
};
const sizeDiv = document.createElement('div');
sizeDiv.className = 'media-size';
sizeDiv.textContent = humanSize(file.size);
const statusDiv = document.createElement('div');
statusDiv.className = 'media-status';
if (file.type.startsWith('image')) {
wrapper.innerHTML = `<img src="${blobUrl}" />`;
if (!firstImageUrl) firstImageUrl = null;
} else if (file.type.startsWith('video')) {
const video = document.createElement('video');
video.src = blobUrl;
video.muted = true;
video.playsInline = true;
video.preload = 'auto';
video.addEventListener('loadeddata', () => {
if (video.readyState >= 2) video.currentTime = 0.5;
});
video.addEventListener('seeked', () => {
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
canvas.toBlob(blob => {
videoPosterBlob = blob;
}, 'image/jpeg', 0.9);
});
wrapper.innerHTML = '';
wrapper.appendChild(video);
}
wrapper.appendChild(removeBtn);
wrapper.appendChild(sizeDiv);
wrapper.appendChild(statusDiv);
previewContainer.insertBefore(wrapper, uploadBox);
mediaFiles.push(file);
mediaElements.push(wrapper);
mediaSizes.push(sizeDiv);
mediaStatusLabels.push(statusDiv);
toggleMetaField();
};
reader.readAsDataURL(file);
}
function toggleMetaField() {
const hasVideo = mediaFiles.some(f => f.type.startsWith('video'));
metaField.style.display = hasVideo ? 'block' : 'none';
}
new Sortable(previewContainer, {
animation: 150,
ghostClass: 'dragging',
filter: '.upload-box',
handle: '.media-box',
delay: 150,
delayOnTouchOnly: true,
onEnd: () => {
const order = [...previewContainer.querySelectorAll('.media-box:not(.upload-box)')];
mediaElements = order;
mediaFiles = order.map(div => mediaFiles.find(f => f.name === div.dataset.filename));
mediaSizes = order.map(div => div.querySelector('.media-size'));
mediaStatusLabels = order.map(div => div.querySelector('.media-status'));
}
});
document.getElementById('custom-post-form').addEventListener('submit', async function(e) {
e.preventDefault();
publishButton.disabled = true;
publishButton.textContent = 'Posting...';
const contentParts = [];
for (let i = 0; i < mediaFiles.length; i++) {
const file = mediaFiles[i];
const sizeDisplay = mediaSizes[i];
const statusDisplay = mediaStatusLabels[i];
sizeDisplay.textContent = humanSize(file.size);
statusDisplay.textContent = 'Uploading...';
const uploadForm = new FormData();
uploadForm.append('action', 'custom_upload_media');
uploadForm.append('media_file', file);
uploadForm.append('_wpnonce', nonce);
const res = await new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', ajaxUrl);
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) {
sizeDisplay.textContent = `${humanSize(e.loaded)} / ${humanSize(e.total)}`;
}
};
xhr.onload = () => resolve(JSON.parse(xhr.responseText));
xhr.onerror = () => resolve({ success: false });
xhr.send(uploadForm);
});
if (!res.success) {
alert('Upload failed.');
publishButton.disabled = false;
publishButton.textContent = 'Publish';
return;
}
const url = res.data.url;
if (file.type.startsWith('image')) {
if (!firstImageUrl) firstImageUrl = url;
contentParts.push(`<img src="${url}" style="max-width:100%">`);
} else if (file.type.startsWith('video')) {
contentParts.push(`[video mp4="${url}"][/video]`);
}
}
document.getElementById('post_content').value = contentParts.join("\n");
const finalContent = contentParts.join("\n");
const formData = new FormData(this);
if (finalContent.includes('<img')) {
formData.set('post_category', 'gallery');
if (firstImageUrl) formData.set('featured_data', firstImageUrl);
} else if (finalContent.includes('[video')) {
formData.set('post_category', 'videos');
if (videoPosterBlob) {
const reader = new FileReader();
reader.onloadend = () => {
formData.set('featured_data', reader.result);
finishPostSubmit(formData);
};
reader.readAsDataURL(videoPosterBlob);
return;
}
} else {
formData.set('post_category', 'text');
}
finishPostSubmit(formData);
});
function finishPostSubmit(formData) {
formData.append('action', 'custom_submit_post');
fetch(ajaxUrl, {
method: 'POST',
body: formData
}).then(res => res.json()).then(data => {
if (data.success) {
publishButton.textContent = 'Posted';
window.location.href = data.data.redirect_url;
} else {
alert(data.data.message || 'Post failed');
publishButton.disabled = false;
publishButton.textContent = 'Publish';
}
});
}
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('custom_post_form', 'custom_post_form_shortcode');