Hot answers tagged

25 votes
Accepted

Can we use PHP in our templates instead of Twig?

No this is not possible. If you really need PHP, you can write a plugin with one of the following: a Template Variable class, which can be accessed from your templates via craft.myPluginHandle a ...
user avatar
  • 8,316
17 votes

Display a copyright year (or range)

You can use Twig's date filter: {{ "now"|date('Y') }} Sitename. All rights reserved. More info in the Twig docs
user avatar
14 votes
Accepted

Is there a quick way to check a server for Craft's minimum requirements?

There is no way of programmatically checking whether a server fulfils the minimum requirements in Craft without uploading all of the Craft files, however I have created a script that performs all of ...
user avatar
  • 6,226
13 votes

How can I check from a plugin config file if devMode is enabled?

For those coming here using Craft 3, it's now Craft::$app->getConfig()->general->devMode.
user avatar
13 votes

How do I upload an asset in Craft 3, via PHP

I finally figured out how to upload an asset, however I still don't understand why the insertFileByLocalPath has been removed. /** * @param UploadedFile $uploadedFile * @param int $folderId * @...
user avatar
10 votes
Accepted

Display a copyright year (or range)

The first two lines of code determine the start year and the current year. The next block of code compares the two years, and sets the copyrightYears variable accordingly. Finally, we simply output ...
user avatar
  • 23.6k
9 votes
Accepted

PHP notice unserialize(): Unexpected end of serialized data

Update: Craft 2.6.2957 added support for PHP 7.1 There's a lot going on here, but 1st thing is 1st. The vagrant box is running PHP 7.1 Craft 2 (and the current latest version of the Yii ...
user avatar
  • 66.4k
8 votes
Accepted

Is there a very very basic example of simply outputting PHP with a plugin to Twig?

Have a look at plugin template variables. You can simply create a method in your plugins variable file that returns anything you want to the template, which would be accessible via {{ craft.myplugin....
user avatar
8 votes
Accepted

How can I check from a plugin config file if devMode is enabled?

It's as simple as if ( craft()->config->get( 'devMode' ) ) { //do stuff }
user avatar
  • 3,833
6 votes
Accepted

How do you set a users group?

It's not documented, but UserGroupsService has a method assignUserToGroups($userId, int|array $groupIds). You'll have to save your user first, but once you have a user id, I believe you can just use ...
user avatar
6 votes
Accepted

Session Cleanup in Craft

Apparently PHP's garbage collection does not work when you've set a custom PHP session save path. Lame. If you set the overridePhpSessionLocation config setting to false, Craft will go with whatever ...
user avatar
  • 66.4k
6 votes
Accepted

Update entry.body from Plugin

There is no event or hook that I'm aware of that can intercept the template rendering process directly. You would likely need to instigate this from within your template. I would suggest perhaps using ...
user avatar
6 votes
Accepted

Plugin Routes: Asterisk Token effect from CP Routes

You can't have tokens in your plugin routes, but you can easily add a segment variable that recreates the asterisk token's functionality with some regex. According to the the official docs on routing, ...
user avatar
6 votes
Accepted

How do I create an advanced route with multiple slugs?

Brad nails the problem in his comment: The error is because you've got two named subpatterns with the same name (<slug>) And the solution is simple... Change the name(s) of your subpattern(s)...
user avatar
  • 23.6k
6 votes
Accepted

Adding new locale - hangs at Step 3 'ResaveAllElements'

You probably need to up Craft's internal memory limit, as well – the following should go into the array in your general.php configuration file: 'phpMaxMemoryLimit' => '1024M',
user avatar
6 votes
Accepted

Where can I run custom PHP scripts?

If you're simply looking to execute some custom scripts that are completely standalone (i.e. they don't interact with the Craft application in any way), those files should live somewhere below your ...
user avatar
6 votes

CraftCMS > UserSessionService should be compatible with CWebUser

It's a known issue you can safely ignore on Craft 2, that's been resolved for Craft 3. PHP 7.0 (or maybe it was 7.1) changed the way that particular error was reported from E_STRICT (which Craft 2 is ...
user avatar
  • 66.4k
5 votes

Setting arbitrary PHP constants in index.php

Hat-tip to Brad for finding this answer. This can be achieved with something like the following in your index.php: define('BRADS_FAVORITE_HOBBY', "Drinking"); Thanks to the power to Twig, you can ...
user avatar
5 votes

Upload any size image into carousel

You shouldn't need to create a custom plugin to do that. You can easily create a Matrix content field for your carousel. Create a new block called slide. From there you can create an asset field to ...
user avatar
5 votes
Accepted

Are there any advantages of updating from PHP 5.4.40 to 5.6?

Short yes! You can find a detailed Benchmark on http://www.lornajane.net/posts/2014/php-5-6-benchmarks. If possible i would try to update from PHP 5.4.40 direct to PHP 7. The performance from PHP 5.4 ...
user avatar
  • 1,317
5 votes

Getting Twig_Error_Runtime when trying to open the website

siteName is automatically global... you don't need to include the global. prefix. <title>{{ siteName }}</title>
user avatar
  • 23.6k
5 votes

Upload file size limits to 256MB

When determining the maximum upload file size, Craft first takes the php.ini file upload_max_filesize, post_max_size and memory_limit into account by getting the minimum value and only then applying ...
user avatar
  • 66.4k
5 votes
Accepted

How do I "register" a service in a Craft 3 plugin?

It turns out there's a handy method for manually registering your services, or in Yii2 parlance, just "components": namespace example\myplugin; use example\myplugin\services\Example as ...
user avatar
5 votes

How do I "register" a service in a Craft 3 plugin?

You can do this in Composer with the following: "extra": { "name": "My Plugin", "handle": "my-plugin", "hasCpSettings": false, "hasCpSection": false, "components": { "...
user avatar
  • 239
5 votes
Accepted

How can I get tasks to complete when not logged in to the CP?

To reiterate what has been said in the comments above and to present an answer, the best way to ensure that queue jobs are completed even without the CMS being logged into or the site being visited is ...
user avatar
  • 6,226
5 votes
Accepted

How to access HTML input values from Twig and vice versa

You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable: https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods {{ ...
user avatar
5 votes
Accepted

Undefined index: siteSettings when updating entry types

Even with 'useProjectConfigFile' => false, it is still active under the hood. Could you run ./craft project-config/rebuild and see if that fixes the issue?
user avatar
  • 7,160
5 votes
Accepted

Is there a way to compare what has changed on an entry while it's being saved?

You're probably looking for Element::getDirtyAttributes() and Element::getDirtyFields()! The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe....
user avatar
4 votes

Session Cleanup in Craft

Here's one answer from cleanup php session files. # Every 30 minutes, not on the hour<br> # Grabs maxlifetime directly from \`php -i\`<br> # doesn't care if /var/lib/php5 exists, errs go ...
user avatar
4 votes
Accepted

Fatal error: Allowed memory size of 134mb exhausted

If your plugin is pulling in a lot of data, then most likely you're just bumping into your php.ini file's memory_limit setting. Try bumping that up to 256M and see if that helps.
user avatar
  • 66.4k

Only top scored, non community-wiki answers of a minimum length are eligible