wp_enqueue_media() and custom post types - image-uploading

I'm attempting to make use of an image uploader, and I have it working in normal posts. However, it does not work on custom post types.
After some searching, it seems I need to call wp_enqueue_media(); somewhere, however, if I do then neither normal posts nor custom post type image uploaders work.
What is the best way to call this function for custom post types?

function media_uploader() {
global $post_type;
if( 'custom-post-type' == $post_type) {
if(function_exists('wp_enqueue_media')) {
wp_enqueue_media();
}
else {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_style('thickbox');
}
}
}
add_action('admin_enqueue_scripts', 'media_uploader');

Related

Is there any way to route random string to dashboard in Codeigniter 3?

I have a question about Codeigniter: Is there any way to check function exists In the controller file and then route to the function path or else route it to a specific url?
I don't know what kind of Dashboard mean in your question.
I just answer how to check function exist or not in controller and route to that function.
In some case, you may need to add a route rule in config/routes.php.
$route['/programme/(.*)$'] = 'programme/programmes/$2';
Use method_exists() to check if function exist.
class Programme extends CI_Controller {
public function programmes($sub_part = NULL) {
if (!empty($sub_part) and method_exists($this, 'programmes_'. $sub_part)) {
return $this->{'programmes_'. $sub_part}(array_slice(func_get_args(), 1));
}
die('bar')
}
private function programmes_foo() {
die('foo')
}
}
When a visitor go to https://www.foobar.com/programme/foo (foo is something affect which function to call), your website should able to show foo. If the function is not exist, it show bar.
You can use
$route['product/(:any)'] = 'yourcontroller/your_function';
Source : https://codeigniter.com/userguide3/general/routing.html
i think you can change your route into this
$route[‘produk/(:num)/(:any)’] = ‘HomeControl/detail_produk/$1/$2’;
result from that route it's like this
http://www.localhost.com/produk/1/nama-produk-1
or maybe you can read the documentation CI 3 in this link or you can read my reference this link
i hope i can help you, thanks.

Getting value with javascript and use it in controller

First of all i am using play framework with scala,
What i have to do is , using the data which i am getting with that function;
onClick: function(node) {
if(!node) return;
alert(node.id);
var id = node.id;
}
So what the question is how i can call the methods from controller with this id ? Since js is client-side but scala works with server-side , i need something creative.
This is the function i need to call from my controller.
def supertrack(id:Long) = Action {
}
I have just found out that i need use ScalaJavaScriptRouting in order to send a ajax request to the server.
For the documentation
http://www.playframework.com/documentation/2.1.1/ScalaJavascriptRouting

silverstripe permissions - prevent dataobject from being edited

at the moment I´m checking out the canEdit and canDelete Functions of Dataobject. As far as I can see I have to call that functions always manually in the template or other php code... Is there a way to prevent editing/deleting in general for a certain Dataobject? When I saw the canEdit function the first time I expected it to be checked by silverstripe automatically before writing the DataObject.
So I just want ADMINS to be allowed to write this DataObject:
public function canEdit($member = null){
return(
Permission::checkMember($member = Member::currentUser(), 'ADMIN')
);
}
Regards,
Florian
public function canEdit($member){
if (Permission::check('ADMIN')){
return true;
}else{
// do something here if applicable
}
}
Reference Link 1
Reference Link 2
Reference Link 3

how to initialize a method parameter with null

I have a custom method that accepts two parameters. I am using this method with several different data sets, of which some only need to have one array passed and other two.. I am wondering if its possible to pass one null if needed?
//method
- (IBAction)startSortingTheArray:(NSArray *)arrayData:(NSArray *)arrayDataB
{
//..
}
Yes, you should be able to pass one null if needed, as long as your implementation is coded to expect it that way. For example:
- (void)startSortingTheArray:(NSArray *)arrayData arrayB:(NSArray *)arrayDataB
{
if (arrayData != nil) {
// process arrayData
}
if (arrayDataB != nil) {
// process arrayDataB
}
}
To make your interface more clean, you could also provide an alternate signature of the method and do something like:
- (void)startSortingTheArray:(NSArray *)arrayData
{
[self startSortingTheArray:arrayData arrayB:nil];
}
Note that I changed the return type from what you initially posted. You had it declared as an IBAction which should take sender as its argument, not an array as you were passing it. I assume you meant for this to be applied to another function and not really to an interface builder action.
Yes you could do that. Then in your startSortingTheArray handle such cases... (i.e. code such a way that you dont assume both arrayData & arrayDataB are present).
Another suggestion I would like to make is if the parameters are getting too many & you have this scenario of some params being present & some not. Then use 1 object as parameter. This object is encapsulated & all the data points are properties of this object. That way your code is much clear & cleaner, easy to maintain blah, blah...
if you are using this method in interface builder e.g attaching it to UIButton then its not possible.
but in if your are calling this method in other methods then yes
[self startSortingTheArray:nil arrayB:nil];
- (IBAction)startSortingTheArray:(NSArray *)arrayData:(NSArray *)arrayDataB
{
if(arrayData == nil){
// do something
}
if(arrayDataB == nil){
// do something
}
if(arryaData == nil && arrayData == nil){
// do something
}
//..
}

How does the session state work in MVC 2.0?

I have a controller that stores various info (Ie. FormID, QuestionAnswerList, etc). Currently I am storing them in the Controller.Session and it works fine.
I wanted to break out some logic into a separate class (Ie. RulesController), where I could perform certain checks, etc, but when I try and reference the Session there, it is null. It's clear that the Session remains valid only within the context of the specific controller, but what is everyone doing regarding this?
I would imagine this is pretty common, you want to share certain "global" variables within the different controllers, what is best practice?
Here is a portion of my code:
In my BaseController class:
public List<QuestionAnswer> QuestionAnswers
{
get
{
if (Session["QuestionAnswers"] == null)
{
List<QuestionAnswer> qAnswers = qaRepository.GetQuestionAnswers(CurrentSection, UserSmartFormID);
Session["QuestionAnswers"] = qAnswers;
return qAnswers;
}
else
{
return (List<QuestionAnswer>)Session["QuestionAnswers"];
}
}
set
{
Session["QuestionAnswers"] = value;
}
}
In my first Controller (derived from BaseController):
QuestionAnswers = qaRepository.GetQuestionAnswers(CurrentSection, UserSmartFormID);
I stepped through the code and the above statement executes fine, setting the Session["QuestionAnswers"], but then when I try to get from another controller below, the Session["QuestionAnswers"] is null!
My second controller (also derived from BaseController):
List<QuestionAnswer> currentList = (List<QuestionAnswer>)QuestionAnswers;
The above line fails! It looks like the Session object itself is null (not just Session["QuestionAnswers"])
does it make a difference if you retrieve your session using
HttpContext.Current.Session("mySpecialSession") ''# note this is VB, not C#
I believe TempData will solve your problem, it operates with in the session and persists across multiple requests, however by default it will clear the stored data once you access it again, if that's a problem you can tell it to keep the info with the newly added Keep() function.
So in your case:
...
TempData["QuestionAnswers"] = qAnswers;
...
There's much more info at:
http://weblogs.asp.net/jacqueseloff/archive/2009/11/17/tempdata-improvements.aspx
Where are you accessing the session in the second controller? The session object is not available in the constructor because it is injected later on in the lifecycle.
Ok, finally got it working, although a bit kludgy. I found the solution from another related SO post.
I added the following to my BaseController:
public new HttpContextBase HttpContext
{
get
{
HttpContextWrapper context =
new HttpContextWrapper(System.Web.HttpContext.Current);
return (HttpContextBase)context;
}
}
Then set/retrieved my Session variables using HttpContext.Session and works fine!