Wordpress Force Post to select category - wordpress

i am using WordPress post to build post.is there any plugin or some function that warn be when i don't select a category for the post.its a headache ever time i post a category i miss some time as there is a lot of post to be made in the category after some time see those post in uncategory its a great headache.
add_theme_support('menus');
**
**> the above code is nothing because stack over flow doesn't allow me to
post my question as it say standard low.**
**
thanks in advance.
hope i could find one suitable answer

hah.. its so funny but you can try as below in your functions.php file
function force_post_categ_init()
{
wp_enqueue_script('jquery');
}
function force_post_categ()
{
echo "<script type='text/javascript'>\n";
echo "
jQuery('#publish').click(function()
{
var cats = jQuery('[id^=\"taxonomy\"]')
.find('.selectit')
.find('input');
category_selected=false;
for (counter=0; counter<cats.length; counter++)
{
if (cats.get(counter).checked==true)
{
category_selected=true;
break;
}
}
if(category_selected==false)
{
alert('You have not selected any category for the post. Please select post category.');
setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
return false;
}
});
";
echo "</script>\n";
}
add_action('admin_init', 'force_post_categ_init');
add_action('edit_form_advanced', 'force_post_categ');
NOte:- javascript must be enable to run this

I would suggest using a jQuery object to read the real value of the input since the page can be loaded with a checked value and then an input can be un-checked and saved. This uses the jQuery object .is('checked') method:
function force_post_categ()
{
$custom_js = <<<CUSTOM_JS
<script type='text/javascript'>
jQuery('#publish').click(function()
{
var cats = jQuery('[id^="taxonomy"]').find('.selectit').find('input');
category_selected = false;
$.each(cats, function(key,value){
if ( $(this).is(':checked') == true ) {
category_selected = true;
return false;
}
});
if (category_selected == false)
{
alert('You have not selected any metro or country for the post. Please select a metro.');
setTimeout("jQuery('#ajax-loading').css('visibility', 'hidden');", 100);
jQuery('[id^="taxonomy"]').find('.tabs-panel').css('background', '#F96');
setTimeout("jQuery('#publish').removeClass('button-primary-disabled');", 100);
return false;
}
});
</script>
CUSTOM_JS;
echo $custom_js;
}

Related

Stop loading posts when no more exist

I'm using the following to load posts into a index page of a wordpress site. The problem is when it gets to the last page and there are no more posts to load. Its just keeps reloading the last page.
Any ideas of how I might stop this from happening? Thanks.
var $content = '#content';
var $nav_wrap = '.navigation';
var $anchor = '.navigation a.next';
var $text = 'Load More';
var $next_href = $($anchor).attr('href'); // Get URL for the next set of posts
$($nav_wrap).html('<a id="almc-load-more" href="' + $next_href + '">' + $text + '</a>');
$('#almc-load-more').click(function(e) {
e.preventDefault();
$.get($(this).attr('href'), '', function(data) {
var $timestamp = new Date().getTime();
var $new_content = $($content, data).wrapInner('<div class="almc-loaded" id="almc-' + $timestamp + '" />').html(); // Grab just the content
$next_href = $($anchor, data).attr('href'); // Get the new href
$('html,body').animate({scrollTop: $($nav_wrap).position().top}, 'slow'); // Animate scroll
$($nav_wrap).before($new_content); // Append the new content
$('#almc-' + $timestamp).hide().fadeIn('slow'); // Animate load
$('#almc-load-more').attr('href', $next_href); // Change the next URL
$('.almc-loaded ' + $nav_wrap).remove(); // Remove the original navigation
});
});
Above code taken from here: http://kaspars.net/blog/wordpress/jquery-script-for-loading-more-posts
You could add some code to check if the new href is different than the current href, and then only try to add a new post if they're different. Then, if they aren't, you could have a message saying there are no more posts.
var lastLink;
$('#almc-load-more').click(function(e) {
if ( $anchor == $('.navigation a.next').last() ) {
lastLink = 1;
}
if (lastLink == 1) {return} else {
...
}
...
After the comment from Mr. Mayers (Thanks) I gave up on the code and used this tutorial:
http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/
Has / does everything I needed.

Fishpig Magento/Wordpress Integration - Appending a WordPress custom menu doesn't set active states

I'm using Fishpigs Wordpress integration module in a Magento store. When I set it to use a custom Wordpress menu, which I've set up in Wordpress with some category hierarchies, it doesn't add any active states if you've clicked a link and are on an "active" page. After digging about, /app/code/community/Fishpig/Wordpress/Model/Menu/Item.php has the following:
public function isItemActive()
{
return false;
}
So it seems like they've just skipped this bit? Anyone any idea how to set active states here?
OK, this seems to do the job, bit of a workaround but hey!
public function isItemActive()
{
$currurl = Mage::helper('core/url')->getCurrentUrl();
$linkurl = $this->getUrl();
if(strstr($linkurl, $currurl)){
return true;
}
else {
return false;
}
}
Get the current url, get the blog url, if they match set active state to true. I then used a bit of jQuery to set states of parents to active as the above only sets the current link:
$('#nav li.nav-3 ul li.level1.active').parent().parent().addClass("active");
...where li.nav-3 is the parent blog link
Replace the isItemActive function with following code in /app/code/community/Fishpig/Wordpress/Model/Menu/Item.php. This is working for me.
public function isItemActive() {
$myblogUrl = Mage::helper('wordpress/abstract')->getBlogRoute();
$mycurrentUrl = preg_replace('/\?.*/', '', Mage::helper('core/url')->getCurrentUrl());
if (in_array($myblogUrl, explode("/", $mycurrentUrl))) {
return true;
} else {
return false;
}
}
The problem is just a simple miss.. Magento uses a "/" in all urls,$currentUrl never matches $currentUrl because of this. The correction is just to trim the "/" I know a late response, but thought it may help someone.
public function isItemActive()
{
$currentUrl = Mage::getUrl('*/*/*', array('_current' => true, '_use_rewrite' => true));
if (strpos($currentUrl, '?') !== false) {
$currentUrl = substr($currentUrl, 0, strpos($currentUrl, '?'));
}
return $currentUrl === rtrim($this->getUrl(), '/');
}

Attaching media to post type without editor support

I'm trying to add attachments to a custom post type that hasn't editor support (only excerpt).
I've managed to show the Media Manager dialog box, but I can only see the "Insert into post" button (that does nothing anyway) and when uploading images, they don't get attached to the post.
To implement what I did so far, I've added a very simple meta box to the post type:
function add_gallery_post_media_meta_box()
{
add_meta_box(
'gallery_post_media',
'Gallery Media',
'gallery_post_media',
'gallery',
'side',
'high'
);
} // add_file_meta_box
add_action('add_meta_boxes', 'add_gallery_post_media_meta_box');
function gallery_post_media()
{
echo '' . __('Add media') .'';
} // end post_media
function register_admin_scripts() {
wp_enqueue_media();
wp_register_script( 'gallery_post_media_admin_script', get_template_directory_uri() . '/library/cpt/gallery.js' );
wp_enqueue_script( 'gallery_post_media_admin_script' );
} // end register_scripts
add_action( 'admin_enqueue_scripts', 'register_admin_scripts' );
And the script:
jQuery(document).ready(function ($) {
$('#gallery-add-media').click(function (e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button', '');
wp.media.editor.send.attachment = function (props, attachment) {
$("#" + id).val(attachment.url);
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open(button);
event.preventDefault();
return false;
});
});
If I would able to find some documentation about wp.media.editor.send.attachment, I'd probably manage to get what I want, but I can't find anything useful.
The only solutions I've found all relies on custom fields, instead I want to simply attach these images to the post, without inserting them in the post content, as I would do with normal posts.
As a side question: is it possible to tell the Media Manager to only accept images?
This is the JavaScript I use for media fields. Once you hit insert you can do whatever you want with the data from the image's selected
jQuery(document).ready(function() {
//uploading files variable
var custom_file_frame;
jQuery(document).on('click', '.meida-manager', function(event) {
event.preventDefault();
$this = jQuery(this);
//If the frame already exists, reopen it
if (typeof(custom_file_frame)!=="undefined") {
custom_file_frame.close();
}
//Create WP media frame.
custom_file_frame = wp.media.frames.customHeader = wp.media({
//Title of media manager frame
title: "Sample title of WP Media Uploader Frame",
library: {
type: 'image'
},
button: {
//Button text
text: "insert text"
},
//Do not allow multiple files, if you want multiple, set true
multiple: false
});
//callback for selected image
custom_file_frame.on('select', function() {
var attachment = custom_file_frame.state().get('selection').first().toJSON();
//do something with attachment variable, for example attachment.filename
//Object:
//attachment.alt - image alt
//attachment.author - author id
//attachment.caption
//attachment.dateFormatted - date of image uploaded
//attachment.description
//attachment.editLink - edit link of media
//attachment.filename
//attachment.height
//attachment.icon - don't know WTF?))
//attachment.id - id of attachment
//attachment.link - public link of attachment, for example ""http://site.com/?attachment_id=115""
//attachment.menuOrder
//attachment.mime - mime type, for example image/jpeg"
//attachment.name - name of attachment file, for example "my-image"
//attachment.status - usual is "inherit"
//attachment.subtype - "jpeg" if is "jpg"
//attachment.title
//attachment.type - "image"
//attachment.uploadedTo
//attachment.url - http url of image, for example "http://site.com/wp-content/uploads/2012/12/my-image.jpg"
//attachment.width
$this.val(attachment.url);
$this.siblings('img').attr('src',attachment.url);
});
//Open modal
custom_file_frame.open();
});
});

EmbeddedForm symfony saving

I'm experiencing a problem to solve the newly added "Prix" in my Soiree Form
I've embedded the PRixForm in my SoireeForm and i'm using ajax calls to add as many prix as we want.
The problem is, when I want to add a new PRix by clicking on the "+" and after filling my fields nothing is saved...
When i'm editing or filling the first default embedded form prix it works which is weird. Only those added by ajax are not saved..
Here is my code
in my form, SoireeForm:
public function addNewPrix($number){
$new_prix = new BaseForm();
for($i=0; $i <= $number; $i+=1){
$pri = new Prix();
$pri->setSoiree($this->getObject());
$prix_form = new PrixForm($pri);
$new_prix ->embedForm($i, $prix_form);
}
$this->embedForm('new', $new_prix);
}
public function saveEmbeddedForm($con = null, $forms = null){
$dataForms = $this->getEmbeddedForm('new')->getEmbeddedForms();
foreach ($dataForms as $dataForm)
$dataForm->getObject()->setSoiree($this->getObject());
parent::saveEmbeddedForm($con, $forms);
}
In my action:
public function executeAdd($request)
{
$this->forward404unless($request->isXmlHttpRequest());
$number = intval($request->getParameter("num"));
$this->form = new SoireeForm();
$this->form->addNewPrix($number);
return $this->renderPartial('addPri',array('form' => $this->form, 'num' => $number));
}
my partial addPri:
<li>
<?php echo $form['new'][$num]['titre']->renderLabel();?>
<?php echo $form['new'][$num]['titre']->render()?>
<?php echo $form['new'][$num]['montant']->renderLabel();?>
<?php echo $form['new'][$num]['montant']->render();?>
<br />
<br />
and my prix.js file:
newfieldscount = 0;
function addPri(num) {
return $.ajax({
type: 'GET',
url: '/backend_dev.php/soiree/add?num='+num,
async: false
}).responseText;
};
var removeNew = function(){
$('.removenew').click(function(e){
e.preventDefault();
$(this).parent().remove();
})
};
$(document).ready(function(){
$('#add_prix').click(function(e) {
e.preventDefault();
$("ul#extraprix").append(addPri(newfieldscount));
newfieldscount = newfieldscount + 1;
$('.removenew').unbind('click');
removeNew();
});
});
I guess the problem comes from my saveEmbed method in my form, but i don't understand why or how to make everything work as it should.
Thank you in advance you guys
So you want save an 1:n relation, so you should find everything you need here
http://www.thatsquality.com/articles/stretching-sfform-with-dynamic-elements-ajax-a-love-story
http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms
If not, please provide information or code of your PrixForm->configure() function !
PS: Your saveEmbeddedForm() function is unneeded, you'll find more information about the form saving process in the second link in chapter 6 !

looping thru a list of checkboxes and saving the values, not working properly

I'm having a bit of a problem with this code.
The program gives me a list of checkboxes but a user ID. then u user can change his selection and push the save button (id="btnSaveUserIntersts") and i am trying to save in the hidden textbox all the values of the checkboxes that was choosen.
The problem is that i am getting all the time the same selections that came form the database and not getting the new selection that the user made.
Can any one tell me what am i doing wrong here?
$(document).ready(
function()
{
$('#btnSaveUserIntersts').bind(
'click',
function()
{
var strCheckBoxChecked = new String();
$('input[type=checkbox][checked]').each(
function()
{
strCheckBoxChecked += $(this).val();
strCheckBoxChecked += ',';
}
);
$('#hidUserInterests').val(strCheckBoxChecked);
}
);
}
);
10x
Try using:
$('input:checkbox:checked').each(
function()
{
strCheckBoxChecked += $(this).val();
strCheckBoxChecked += ',';
}
);
As the selector instead of what you're currently using.
$('input[type="checkbox"]:checked')
Use .map, it's far prettier:
var strCheckBoxChecked = $('input:checkbox:checked').map(function()
return this.value;
}).get().join(",");
And the selector you are using is close, $('input[type=checkbox][checked=checked]')

Resources